{"id":51,"date":"2019-06-25T20:33:55","date_gmt":"2019-06-25T20:33:55","guid":{"rendered":"https:\/\/www.practicalpowershell.com\/post\/powershell-parameters-basic"},"modified":"2020-03-09T19:31:35","modified_gmt":"2020-03-10T01:31:35","slug":"powershell-parameters-basic","status":"publish","type":"post","link":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/","title":{"rendered":"PowerShell Parameters (Basic)"},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?resize=150%2C150&#038;ssl=1\" alt=\"\" width=\"150\" height=\"150\" class=\"alignleft size-thumbnail wp-image-345\" srcset=\"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?resize=100%2C100&amp;ssl=1 100w, https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?zoom=2&amp;resize=150%2C150&amp;ssl=1 300w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a>Why use external arguments for PowerShell scripts? External parameters allow for options to be selected that correspond to functions or actions within the script. For example, if you wanted to trigger a HTML report for output, maybe we would specify a parameter of &#8216;Report&#8217;. <BR><br \/>\n<strong>Example 1<\/strong><BR><br \/>\nRun from PowerShell:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nGet-ExchangeHealth.ps1 Office365<br \/>\n[\/sourcecode]<br \/>\nBy running this<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$Switch1 = $args[0]<br \/>\n[\/sourcecode]<br \/>\nLater in the script we can then use the parameter to make a choice:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf (Switch1 -ne &#039;Office365) {<br \/>\n    (Get-ExchangeServer).Name<br \/>\n} Else {<br \/>\n    Write-Verbose &#039;Connected to Exchange Online, no Exchange Servers to query&#039;<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\n<strong>Example 2<\/strong><BR><br \/>\nRun from PowerShell:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nGet-ExchangeHealth.ps1 Office365 MailboxStats<br \/>\n[\/sourcecode]<br \/>\nNow not only do we get to connect to Office 365, but we can also query mailbox stats in the same run.<BR><br \/>\nCode in the script:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nparam(<br \/>\n    [Parameter(Mandatory=$false,ParameterSetName=&quot;Office365&quot;)] [switch]$Office365,<br \/>\n    [Parameter(Mandatory=$false,ParameterSetName=&quot;MailboxStats&quot;)] [switch]$MailboxStats<br \/>\n)<br \/>\n[\/sourcecode]<br \/>\nLater in the script we would have two code blocks to handle these requests: (not a complete sample)<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($Environment -eq &#039;Office365&#039;) { Write-host &#039; &#039; Write-host &#039; *** &#039; -ForegroundColor White -NoNewline Write-host &#039;Script will examine Exchange Online now.&#039; -ForegroundColor Red -NoNewline Write-host &#039; *** &#039; -ForegroundColor White Write-host &#039; &#039; $ExOMenu = { Write-Host &quot; ***********************************&quot; -ForegroundColor White Write-Host &quot; Exchange Online Health Check Menu&quot; -ForegroundColor Cyan Write-Host &quot; ***********************************&quot; -ForegroundColor White<br \/>\n[\/sourcecode]<br \/>\n\u2026 and so on\u2026 Then, code for the mailbox stats options:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($MailboxStats) {<br \/>\n    Get-mailbox | Get-MailboxStatistics |  select-object DisplayName, {$_.TotalItemSize.Value.ToMB()}<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\n<strong>Example 3 <\/strong><br \/>\nRun from PowerShell:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nGet-ExchangeHealth.Ps1 SecurityComplianceCenter<br \/>\n[\/sourcecode]<br \/>\nThen we have this code in the script to look for this switch:<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\n$SCC = $args[0]<br \/>\n[\/sourcecode]<br \/>\nThen later in the script, we can check to see if the parameter populated this variable and if so, execute some code (sample below):<br \/>\n[sourcecode language=&#8221;powershell&#8221;]<br \/>\nIf ($SCC) {<br \/>\n$LiveCred = Get-Credential<br \/>\n$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https:\/\/ps.compliance.protection.outlook.com\/powershell-liveid\/ -Credential $LiveCred -Authentication Basic -AllowRedirection<br \/>\nImport-PSSession $Session<br \/>\nGet-RetentionComplianceTag<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nThus we can use parameters to have our script handle additional tasks when executing. The above examples are simplistic, but do provide an introductory view into how they can be utilized.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why use external arguments for PowerShell scripts? External parameters allow for options to be selected that correspond to functions or actions within the script. For example, if you wanted to trigger a HTML report for output, maybe we would specify a parameter of &#8216;Report&#8217;. Example 1 Run from PowerShell: [sourcecode language=&#8221;powershell&#8221;] Get-ExchangeHealth.ps1 Office365 [\/sourcecode] By [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":[],"rop_publish_now_history":[],"rop_publish_now_status":"pending","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell Parameters (Basic) - Practical PowerShell<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Parameters (Basic) - Practical PowerShell\" \/>\n<meta property=\"og:description\" content=\"Why use external arguments for PowerShell scripts? External parameters allow for options to be selected that correspond to functions or actions within the script. For example, if you wanted to trigger a HTML report for output, maybe we would specify a parameter of &#8216;Report&#8217;. Example 1 Run from PowerShell: [sourcecode language=&#8221;powershell&#8221;] Get-ExchangeHealth.ps1 Office365 [\/sourcecode] By [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical PowerShell\" \/>\n<meta property=\"article:published_time\" content=\"2019-06-25T20:33:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-10T01:31:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter-150x150.jpg\" \/>\n<meta name=\"author\" content=\"damian\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"damian\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/\"},\"author\":{\"name\":\"damian\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"headline\":\"PowerShell Parameters (Basic)\",\"datePublished\":\"2019-06-25T20:33:55+00:00\",\"dateModified\":\"2020-03-10T01:31:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/\"},\"wordCount\":365,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/Parameter-150x150.jpg\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/\",\"name\":\"PowerShell Parameters (Basic) - Practical PowerShell\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/Parameter-150x150.jpg\",\"datePublished\":\"2019-06-25T20:33:55+00:00\",\"dateModified\":\"2020-03-10T01:31:35+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#primaryimage\",\"url\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/Parameter.jpg?fit=336%2C195&ssl=1\",\"contentUrl\":\"https:\\\/\\\/i0.wp.com\\\/practicalpowershell.com\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/Parameter.jpg?fit=336%2C195&ssl=1\",\"width\":336,\"height\":195},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/powershell-parameters-basic\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/practicalpowershell.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Parameters (Basic)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#website\",\"url\":\"https:\\\/\\\/practicalpowershell.com\\\/\",\"name\":\"Practical PowerShell\",\"description\":\"PowerShell books written by experts\",\"publisher\":{\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/practicalpowershell.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/practicalpowershell.com\\\/#\\\/schema\\\/person\\\/4d0733c81966e744aabbb49f56d64deb\",\"name\":\"damian\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\",\"caption\":\"damian\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Parameters (Basic) - Practical PowerShell","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Parameters (Basic) - Practical PowerShell","og_description":"Why use external arguments for PowerShell scripts? External parameters allow for options to be selected that correspond to functions or actions within the script. For example, if you wanted to trigger a HTML report for output, maybe we would specify a parameter of &#8216;Report&#8217;. Example 1 Run from PowerShell: [sourcecode language=&#8221;powershell&#8221;] Get-ExchangeHealth.ps1 Office365 [\/sourcecode] By [&hellip;]","og_url":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/","og_site_name":"Practical PowerShell","article_published_time":"2019-06-25T20:33:55+00:00","article_modified_time":"2020-03-10T01:31:35+00:00","og_image":[{"url":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter-150x150.jpg","type":"","width":"","height":""}],"author":"damian","twitter_card":"summary_large_image","twitter_misc":{"Written by":"damian","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#article","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/"},"author":{"name":"damian","@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"headline":"PowerShell Parameters (Basic)","datePublished":"2019-06-25T20:33:55+00:00","dateModified":"2020-03-10T01:31:35+00:00","mainEntityOfPage":{"@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/"},"wordCount":365,"commentCount":0,"publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"image":{"@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter-150x150.jpg","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/","url":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/","name":"PowerShell Parameters (Basic) - Practical PowerShell","isPartOf":{"@id":"https:\/\/practicalpowershell.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#primaryimage"},"image":{"@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#primaryimage"},"thumbnailUrl":"https:\/\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter-150x150.jpg","datePublished":"2019-06-25T20:33:55+00:00","dateModified":"2020-03-10T01:31:35+00:00","breadcrumb":{"@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/practicalpowershell.com\/powershell-parameters-basic\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#primaryimage","url":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?fit=336%2C195&ssl=1","contentUrl":"https:\/\/i0.wp.com\/practicalpowershell.com\/wp-content\/uploads\/2019\/06\/Parameter.jpg?fit=336%2C195&ssl=1","width":336,"height":195},{"@type":"BreadcrumbList","@id":"https:\/\/practicalpowershell.com\/powershell-parameters-basic\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/practicalpowershell.com\/"},{"@type":"ListItem","position":2,"name":"PowerShell Parameters (Basic)"}]},{"@type":"WebSite","@id":"https:\/\/practicalpowershell.com\/#website","url":"https:\/\/practicalpowershell.com\/","name":"Practical PowerShell","description":"PowerShell books written by experts","publisher":{"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/practicalpowershell.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/practicalpowershell.com\/#\/schema\/person\/4d0733c81966e744aabbb49f56d64deb","name":"damian","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g","caption":"damian"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/d5a8cc64a5aa27558a897b108e3be1a89859511a3fd26176dac292f26e7a4ae4?s=96&d=mm&r=g"}}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/51","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":2,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"predecessor-version":[{"id":347,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/posts\/51\/revisions\/347"}],"wp:attachment":[{"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/practicalpowershell.com\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}