{"id":78719,"date":"2026-06-27T18:05:41","date_gmt":"2026-06-27T18:05:41","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78719"},"modified":"2026-06-27T18:05:41","modified_gmt":"2026-06-27T18:05:41","slug":"cmdletbinding","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78719","title":{"rendered":"CmdletBinding"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ref: AI Tools\/ChatGPT<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>[CmdletBinding()]<\/code> turns a normal PowerShell function into an <strong>advanced function<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That means the function behaves more like a real PowerShell cmdlet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Normal function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Install-Office {\n    param(\n        &#91;string]$Version\n    )\n\n    Write-Host \"Installing Office $Version\"\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Advanced function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Install-Office {\n    &#91;CmdletBinding()]\n    param(\n        &#91;string]$Version\n    )\n\n    Write-Host \"Installing Office $Version\"\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What <code>[CmdletBinding()]<\/code> does<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It gives your function cmdlet-style behavior, such as:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>Common parameters<\/td><td>Allows parameters like <code>-Verbose<\/code>, <code>-Debug<\/code>, <code>-ErrorAction<\/code><\/td><\/tr><tr><td>Better parameter handling<\/td><td>Works better with <code>[Parameter()]<\/code>, mandatory parameters, parameter sets<\/td><\/tr><tr><td>Pipeline support<\/td><td>Works well with <code>ValueFromPipeline<\/code> and <code>process {}<\/code><\/td><\/tr><tr><td>Professional structure<\/td><td>Makes custom functions look and behave more like built-in cmdlets<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Example with <code>-Verbose<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function Test-LabFunction {\n    &#91;CmdletBinding()]\n    param(\n        &#91;string]$Name\n    )\n\n    Write-Verbose \"The function received the name $Name\"\n    Write-Host \"Hello $Name\"\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run normally:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Test-LabFunction -Name \"Sayed\"\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hello Sayed\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run with <code>-Verbose<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Test-LabFunction -Name \"Sayed\" -Verbose\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>VERBOSE: The function received the name Sayed\nHello Sayed\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Without <code>[CmdletBinding()]<\/code>, <code>-Verbose<\/code> would not work the same way.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example with mandatory parameter<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>function Install-Office {\n    &#91;CmdletBinding()]\n    param(\n        &#91;Parameter(Mandatory)]\n        &#91;ValidateSet(\"2013\",\"2016\")]\n        &#91;string]$Version\n    )\n\n    Write-Host \"I installed Office $Version.\"\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now if the user runs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Install-Office\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">PowerShell asks for the missing mandatory value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cmdlet Install-Office at command pipeline position 1\nSupply values for the following parameters:\nVersion:\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Simple explanation for students<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><code>[CmdletBinding()]<\/code> upgrades a function into an advanced PowerShell function. It allows the function to support cmdlet-like features such as <code>-Verbose<\/code>, better parameter behavior, validation, parameter sets, and pipeline-friendly design.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Very short version<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;CmdletBinding()]\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">means:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u201cTreat this function like a professional PowerShell cmdlet.\u201d<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Ref: AI Tools\/ChatGPT [CmdletBinding()] turns a normal PowerShell function into an advanced function. That means the function behaves more like a real PowerShell cmdlet. Basic example Normal function: Advanced function: What [CmdletBinding()] does It gives your function cmdlet-style behavior, such as: Feature Meaning Common parameters Allows parameters like -Verbose, -Debug, -ErrorAction Better parameter handling Works &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=78719\">Continue reading<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1981],"tags":[],"class_list":["post-78719","post","type-post","status-publish","format-standard","hentry","category-power-shell","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":78795,"url":"http:\/\/bangla.sitestree.com\/?p=78795","url_meta":{"origin":78719,"position":0},"title":"ValidateNotNullOrEmpty","author":"Sayed","date":"July 13, 2026","format":false,"excerpt":"[ValidateNotNullOrEmpty()] is a PowerShell parameter-validation attribute. It rejects values that are: $null An empty string: \"\" An empty collection Example: function Show-ComputerName { param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string]$ComputerName ) Write-Output \"Computer name: $ComputerName\" } Valid call: Show-ComputerName -ComputerName \"PC01\" Output: Computer name: PC01 Invalid empty value: Show-ComputerName -ComputerName \"\" Invalid null\u2026","rel":"","context":"In &quot;Power Shell&quot;","block_context":{"text":"Power Shell","link":"http:\/\/bangla.sitestree.com\/?cat=1981"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78793,"url":"http:\/\/bangla.sitestree.com\/?p=78793","url_meta":{"origin":78719,"position":1},"title":"Write-Error","author":"Sayed","date":"July 13, 2026","format":false,"excerpt":"Write-Error in PowerShell Write-Error writes an error message to PowerShell\u2019s error stream. By default, it creates a non-terminating error. That means PowerShell displays the error, but usually continues with the next command. Write-Error \"The file could not be found.\" Write-Output \"The script continued.\" Typical result: Write-Error: The file could not\u2026","rel":"","context":"In &quot;Power Shell&quot;","block_context":{"text":"Power Shell","link":"http:\/\/bangla.sitestree.com\/?cat=1981"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":24412,"url":"http:\/\/bangla.sitestree.com\/?p=24412","url_meta":{"origin":78719,"position":2},"title":"Good Coding Style in PHP + Other languages #Root","author":"Author-Check- Article-or-Video","date":"April 8, 2021","format":false,"excerpt":"Style 1.1: Use proper indenting while ($x < $z) { if ($a == 1) { echo 'A was equal to 1'; } else { if ($b == 2) { \/\/do something } else { \/\/do something else } } } \u00a0 1.2 while ($x < $z) { if ($a ==\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":70111,"url":"http:\/\/bangla.sitestree.com\/?p=70111","url_meta":{"origin":78719,"position":3},"title":"Good Coding Style in PHP + Other languages #15","author":"Author-Check- Article-or-Video","date":"August 26, 2021","format":false,"excerpt":"Style 1.1: Use proper indentingwhile ($x < $z) { if ($a == 1) { echo 'A was equal to 1'; } else { if ($b == 2) { \/\/do something } else { \/\/do something else } }}1.2while ($x < $z) { if ($a == 1) { echo 'A was\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78663,"url":"http:\/\/bangla.sitestree.com\/?p=78663","url_meta":{"origin":78719,"position":4},"title":"write-output vs write-host","author":"Sayed","date":"May 24, 2026","format":false,"excerpt":"Write-Output vs Write-Host in PowerShell FeatureWrite-OutputWrite-HostSends data to pipeline\u2705 Yes\u274c NoCan be stored in variable\u2705 Yes\u274c Usually noCan be redirected to file\u2705 Yes\u274c Not normally usefulUsed for script output\u2705 Recommended\u26a0\ufe0f Mainly for display messagesSupports formatting\/colorLimited\u2705 Good for colors 1. Write-Output Write-Output sends data to the PowerShell pipeline. Write-Output \"Hello PowerShell\"\u2026","rel":"","context":"In &quot;Power Shell&quot;","block_context":{"text":"Power Shell","link":"http:\/\/bangla.sitestree.com\/?cat=1981"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78335,"url":"http:\/\/bangla.sitestree.com\/?p=78335","url_meta":{"origin":78719,"position":5},"title":"Java: ServletConfig vs ServletContext","author":"Sayed","date":"July 27, 2025","format":false,"excerpt":"Here\u2019s a clear and copyright-free comparison of ServletConfig vs ServletContext \u2014 perfect for learning or blogging. \ud83d\udd04 ServletConfig vs ServletContext in Java Both ServletConfig and ServletContext are part of the Java Servlet API and provide access to configuration data, but they serve different scopes and purposes. \ud83d\udccb Comparison Table FeatureServletConfigServletContextScopePer\u2026","rel":"","context":"In &quot;Anything JAVA&quot;","block_context":{"text":"Anything JAVA","link":"http:\/\/bangla.sitestree.com\/?cat=1975"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78719"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78719\/revisions"}],"predecessor-version":[{"id":78720,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78719\/revisions\/78720"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78719"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}