{"id":85197,"date":"2019-09-17T15:27:38","date_gmt":"2019-09-17T15:27:38","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=85197"},"modified":"2026-03-18T11:25:24","modified_gmt":"2026-03-18T11:25:24","slug":"how-to-use-parameters-in-powershell","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/how-to-use-parameters-in-powershell\/","title":{"rendered":"PowerShell Parameters: A Practical Guide"},"content":{"rendered":"<p><strong>The series so far:<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/how-to-use-parameters-in-powershell\/\">How to Use Parameters in PowerShell Part I<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/how-to-use-parameters-in-powershell-part-ii\/\">How to User Parameters in PowerShell Part II<\/a><\/li>\n<\/ol>\n\n\n\n\n<p>PowerShell provides two main ways to accept parameters in a script: unnamed arguments using the $args array, and named parameters using the Param() block. Named parameters are the recommended approach because they provide type safety, built-in help messages, mandatory enforcement, and default values &#8211; making scripts more readable and portable across environments. This guide walks through both methods with working examples you can run immediately.<\/p>\n\n\n\n<p><strong>Introduction<\/strong><\/p>\n\n\n\n<p>Recently I had a client ask me to update a script in both production and UAT. He wanted any emails sent out to include the name of the environment. It was a simple request, and I supplied a simple solution. I just created a new variable:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$envname = \"UAT\"<\/pre>\n\n\n\n<p>After updating the script for the production environment, I then modified the subject line for any outgoing emails to include the new variable.<\/p>\n\n\n\n<p>At the time though, I wanted to do this in a better way, and not just for this variable, but also for the others I use in the script. When I wrote this script, it was early in my days of writing PowerShell, so I simply hardcoded variables into it. It soon became apparent that this was less than optimal when I needed to move a script from Dev\\UAT into production because certain variables would need to be updated between the environments.<\/p>\n\n\n\n<p>Fortunately, like most languages, PowerShell permits the use of parameters, but, like many things in PowerShell, there\u2019s more than one way of doing it. I will show you how to do it in two different ways and discuss why I prefer one method over the other.<br><strong>Read also: <\/strong><a href=\"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/high-performance-powershell-linq\/\">Using LINQ for high-performance PowerShell<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-let-s-have-an-argument\">Let\u2019s Have an Argument<\/h2>\n\n\n\n<p>The first and arguably (see what I did there) the easiest way to get command line arguments is to write something like the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$param1=$args[0]\nwrite-host $param1<\/pre>\n\n\n\n<p>If you run this from within the PowerShell ISE by pressing <em>F5<\/em>, nothing interesting will happen. This is a case where you will need to run the saved file from the ISE Console and supply a value for the argument.<\/p>\n\n\n\n<p>To make sure PowerShell executes what you want, navigate in the command line to the same directory where you will save your scripts. Name the script <em>Unnamed_Arguments_Example_1.ps1<\/em> and run it with the argument <em>FOO<\/em>. It will echo back <em>FOO<\/em>.&nbsp;<span style=\"display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: Georgia,'Times New Roman','Bitstream Charter',Times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">(The scripts for this article can be found <\/span><a href=\"https:\/\/github.com\/stridergdm\/SimpleTalk_PowerShell-Scripts\/tree\/master\/Parameter%20Passing%20Examples\">here<\/a><span style=\"display: inline !important; float: none; background-color: #ffffff; color: #333333; cursor: text; font-family: Georgia,'Times New Roman','Bitstream Charter',Times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;\">.)<\/span><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> .\\Unnamed_Arguments_Example_1.ps1 FOO<\/pre>\n\n\n\n<p>You\u2019ve probably already guessed that since <code>$args<\/code> is an array, you can access multiple values from the command line.<\/p>\n\n\n\n<p>Save the following script as <em>Unnamed_Arguments_Example_2.ps1.&nbsp;<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$servername=$args[0]\n$envname=$args[1]\nwrite-host \"If this script were really going to do something, it would do it on $servername in the $envname environment\"<\/pre>\n\n\n\n<p>Run it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Unnamed_Arguments_Example_2.ps1 HAL Odyssey<\/pre>\n\n\n\n<p>You should see:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"79\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-12.png\" alt=\"\" class=\"wp-image-85198\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>One nice ability of reading the arguments this way is that you can pass in an arbitrary number of arguments if you need to. Save the following example as <em>Unnamed_Arguments_Example_3.ps1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">write-host \"There are a total of $($args.count) arguments\"\nfor ( $i = 0; $i -lt $args.count; $i++ ) {\n    write-host \"Argument  $i is $($args[$i])\"\n}<\/pre>\n\n\n\n<p>If you call it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Unnamed_Arguments_Example_3.ps1 foo bar baz<\/pre>\n\n\n\n<p>You should get:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1003\" height=\"121\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-13.png\" alt=\"\" class=\"wp-image-85199\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>The method works, but I would argue that it\u2019s not ideal. For one thing, you can accidentally pass in parameters in the wrong order. For another, it doesn\u2019t provide the user with any useful feedback. I will outline the preferred method below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-using-named-parameters\">Using Named Parameters<\/h2>\n\n\n\n<p>Copy the following script and save it as <em>Named_Parameters_Example_1.ps1<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param ($param1)\nwrite-host $param1<\/pre>\n\n\n\n<p>Then run it.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_1.ps1<\/pre>\n\n\n\n<p>When you run it like this, nothing will happen.<\/p>\n\n\n\n<p>But now enter:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_1.ps1 test<\/pre>\n\n\n\n<p>And you will see your script echo back the word <em>test<\/em>.<\/p>\n\n\n\n<p>This is what you might expect, but say you had multiple parameters and wanted to make sure you had assigned the right value to each one. You might have trouble remembering their names and perhaps their order. But that\u2019s OK; PowerShell is smart. Type in the same command as above but add a dash (-) at the end.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_1.ps1 -<\/pre>\n\n\n\n<p>PowerShell should now pop up a little dropdown that shows you the available parameters. In this case, you only have the one parameter, <code>param1<\/code>. Hit tab to autocomplete and enter the word <em>test<\/em> or any other word you want, and you should see something similar to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_1.ps1 -param1 test<\/pre>\n\n\n\n<p>Now if you hit enter, you will again see the word <em>test <\/em>echoed.<\/p>\n\n\n\n<p>If you run the script from directly inside PowerShell itself, as opposed to the ISE, tab completion will still show you the available parameters, but will not pop them up in a nice little display.<\/p>\n\n\n\n<p>Create and save the following script as <em>Named_Parameters_Example_2.ps1<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param ($servername, $envname)\nwrite-host \"If this script were really going to do something, it would do it on $servername in the $envname environment\"<\/pre>\n\n\n\n<p>Note now you have two parameters.<\/p>\n\n\n\n<p>By default, PowerShell will use the position of the parameters in the file to determine what the parameter is when you enter it. This means the following will work:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_2.ps1 HAL Odyssey<\/pre>\n\n\n\n<p>The result will be:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1001\" height=\"77\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-14.png\" alt=\"\" class=\"wp-image-85200\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Here\u2019s where the beauty of named parameters shines. Besides not having to remember what parameters the script may need, you don\u2019t have to worry about the order in which they\u2019re entered.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_2.ps1 -envname Odyssey -servername HAL<\/pre>\n\n\n\n<p>This will result in the exact same output as above, which is what you should expect:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1003\" height=\"80\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-15.png\" alt=\"\" class=\"wp-image-85201\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>If you used tab completion to enter the parameter names, what you will notice is once you\u2019ve entered a value for one of the parameters (such as <code>\u2013envname<\/code> above), when you try to use tab completion for another parameter, only the remaining parameters appear. In other words, PowerShell won\u2019t let you enter the same parameter twice if you use tab completion.<\/p>\n\n\n\n<p>If you do force the same parameter name twice, PowerShell will give you an error similar to:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"140\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-16.png\" alt=\"\" class=\"wp-image-85202\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>One question that probably comes to mind at this point is how you would handle a parameter with a space in it. For example, how would you enter a file path like <em>C:\\path to file\\File.ext<\/em>.<\/p>\n\n\n\n<p>The answer is simple; you can wrap the parameter in quotes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_2.ps1 -servername HAL -envname 'USS Odyssey'<\/pre>\n\n\n\n<p>The code will result in:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1004\" height=\"78\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-17.png\" alt=\"\" class=\"wp-image-85203\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>With the flexibility of PowerShell and quoting, you can do something like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_2.ps1 -servername HAL -envname \"'USS Odyssey'\"<\/pre>\n\n\n\n<p>You\u2019ll see this message back:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1003\" height=\"79\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-18.png\" alt=\"\" class=\"wp-image-85204\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>If you experiment with entering different values into the scripts above, you\u2019ll notice that it doesn\u2019t care if you type in a string or a number or pretty much anything you want. This may be a problem if you need to control the type of data the user is entering.<\/p>\n\n\n\n<p>This leads to typing of your parameter variables. I generally do not do this for variables within PowerShell scripts themselves (because in most cases I\u2019m controlling how those variables are being used), but I almost always ensure typing of my parameter variables so I can have some validation over my input.<\/p>\n\n\n\n<p>Consider why this may be important. Save the following script as <em>Named_Parameters_Example_3.ps1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param ([int] $anInt, $maybeanInt)\nwrite-host \"Adding $anint to itself results in: $($anInt + $anInt)\"\nwrite-host \"But trying to add $maybeanInt to itself results in: $($maybeanInt + $maybeanInt)\"<\/pre>\n\n\n\n<p>Now run it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_3.ps1 -anInt 5 -maybeanInt 6<\/pre>\n\n\n\n<p>You will get the results you expect:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"77\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-19.png\" alt=\"\" class=\"wp-image-85205\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>What if you don\u2019t control the data being passed, and the passing program passes items in quoted strings?<\/p>\n\n\n\n<p>To simulate that run the script with a slight modification:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_3.ps1 -anInt \"5\" -maybeanInt \"6\"<\/pre>\n\n\n\n<p>This will result in:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"80\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-20.png\" alt=\"\" class=\"wp-image-85206\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>If you instead declare <code>$maybeanInt<\/code> as an <code>[int]<\/code> like you did <code>$anInt<\/code>, you can assure the two get added together, not concatenated.<\/p>\n\n\n\n<p>However, keep in mind if someone tries to call the same script with an actual string such as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_3.ps1 Foo 6<\/pre>\n\n\n\n<p>It will return a gross error message, so this can be a double-edged sword.<\/p>\n\n\n\n<section id=\"my-first-block-block_74ba68613472c3c73677a77d8a343050\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Automate your SQL workflows with PowerShell and Redgate<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            The PowerShell parameters you\u2019re learning here can do more than automate simple tasks. With tools like Redgate Monitor, SQL Clone, SQL Change Automation, SQL Data Catalog, and Redgate Flyway, you can monitor servers, create clones, deploy database changes, and more &#8211; all from your scripts.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/products\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Explore the full portfolio: Automate your SQL workflows with PowerShell and Redgate\">Explore the full portfolio<\/a>\n                    <\/div>\n    <\/div>\n<\/section>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-using-defaults\">Using Defaults<\/h2>\n\n\n\n<p>When running a script, I prefer to make it require as little typing as possible and to eliminate errors where I can. This means that I try to use defaults.<\/p>\n\n\n\n<p>Modify the <em>Named_Parameters_Example_2.ps1<\/em> script as follows and save it as <em>Named_Parameters_Example_4.ps1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param ($servername, $envname='Odyssey')\nwrite-host \"If this script were really going to do something, it would do it on $servername in the $envname environment\"<\/pre>\n\n\n\n<p>And then run it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_4.ps1 -servername HAL<\/pre>\n\n\n\n<p>Do not bother to enter the environment name. You should get:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"80\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2019\/09\/word-image-21.png\" alt=\"\" class=\"wp-image-85207\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>This isn\u2019t much savings in typing but does make it a bit easier and does mean that you don\u2019t have to remember how to spell <em>Odyssey<\/em>!<\/p>\n\n\n\n<p>There may also be cases where you don\u2019t want a default parameter, but you absolutely want to make sure a value is entered. You can do this by testing to see if the parameter is null and then prompting the user for input.<\/p>\n\n\n\n<p>Save the following script as <em>Named_Parameters_Example_5.ps1<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param ($servername, $envname='Odyssey')\nif ($servername -eq $null) {\n$servername = read-host -Prompt \"Please enter a servername\" \n}\nwrite-host \"If this script were really going to do something, it would do it on $servername in the $envname environment\"<\/pre>\n\n\n\n<p>You will notice that this combines both, a default parameter and testing the see if the <code>$servername<\/code> is null and if it is, prompting the user to enter a value.<\/p>\n\n\n\n<p>You can run this from the command line in multiple ways:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_5.ps1 -servername HAL<\/pre>\n\n\n\n<p>It will do exactly what you think: use the passed in servername value of <em>HAL<\/em> and the default environment of <em>Odyssey<\/em>.<\/p>\n\n\n\n<p>But you could also run it as:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_5.ps1 -envname Discovery<\/pre>\n\n\n\n<p>And in this case, it will override the default parameter for the environment with <em>Discovery<\/em>, and it will prompt the user for the computer name. To me, this is the best of both worlds.<\/p>\n\n\n\n<p>There is another way of ensuring your users enter a parameter when it\u2019s mandatory.<\/p>\n\n\n\n<p>Save the following as <em>Named_Parameters_Example_6.ps1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param ([Parameter(Mandatory)]$servername, $envname='Odessey')\nwrite-host \"If this script were really going to do something, it would do it on $servername in the $envname environment\"<\/pre>\n\n\n\n<p>and run it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_6.ps1<\/pre>\n\n\n\n<p>You\u2019ll notice it forces you to enter the servername because you made that mandatory, but it still used the default environment name of <em>Odyssey<\/em>.<\/p>\n\n\n\n<p>You can still enter the parameter on the command line too:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_6.ps1  -servername HAL -envname Discovery<\/pre>\n\n\n\n<p>And PowerShell won\u2019t prompt for the servername since it\u2019s already there.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-using-an-unknown-number-of-arguments\">Using an Unknown Number of Arguments<\/h2>\n\n\n\n<p>Generally, I find using named parameters far superior over merely reading the arguments from the command line. One area that reading the arguments is a tad easier is when you need the ability to handle an unknown number of arguments.<\/p>\n\n\n\n<p>For example, save the following script as <em>Unnamed_Arguments_Example_4.ps1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">write-host \"There are a total of $($args.count) arguments\"\nfor ( $i = 0; $i -lt $args.count; $i++ ) {\n    $diskdata = get-PSdrive $args[$i] | Select-Object Used,Free\n    write-host \"$($args[$i]) has  $($diskdata.Used) Used and $($diskdata.Free) free\"\n}<\/pre>\n\n\n\n<p>Then call it as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Unnamed_Arguments_Example_4.ps1 C D E<\/pre>\n\n\n\n<p>You will get back results for the amount of space free on the drive letters you list. As you can see, you can enter as many drive letters as you want.<\/p>\n\n\n\n<p>One attempt to write this using named parameters might look like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param($drive1, $drive2, $drive3)\n$diskdata = get-PSdrive $drive1 | Select-Object Used,Free\nwrite-host \"$($drive1) has  $($diskdata.Used) Used and $($diskdata.Free) free\"\nif ($drive2 -ne $null) {\n$diskdata = get-PSdrive $drive2 | Select-Object Used,Free\nwrite-host \"$($drive2) has  $($diskdata.Used) Used and $($diskdata.Free) free\"\n    if ($drive3 -ne $null) {\n    $diskdata = get-PSdrive $drive3 | Select-Object Used,Free\n    write-host \"$($drive3) has  $($diskdata.Used) Used and $($diskdata.Free) free\"\n    }\n    else\n    { return}\n}\nelse\n{return} # don't bother testing for drive3 since we didn't even have drive 3<\/pre>\n\n\n\n<p>As you can see, that gets ugly fairly quickly as you would have to handle up to 26 drive letters.<\/p>\n\n\n\n<p>Fortunately, there\u2019s a better way to handle this using named parameters. Save the following as <em>Named_Parameters_Example_7.ps1<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">param($drives)\nforeach ($drive in $drives) \n{\n    $diskdata = get-PSdrive $drive | Select-Object Used,Free\n    write-host \"$($drive) has  $($diskdata.Used) Used and $($diskdata.Free) free\"\n}<\/pre>\n\n\n\n<p>If you want to check the space on a single drive, then you call this as you would expect:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_7.ps1 C<\/pre>\n\n\n\n<p>On the other hand, if you want to test multiple drives, you can pass an array of strings.<\/p>\n\n\n\n<p>This can be done one of two ways:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_7.ps1 C,D,E<\/pre>\n\n\n\n<p>Note that there are commas separating the drive letters, not spaces. This lets PowerShell know that this is all one parameter. (An interesting side note: if you do put a space after comma, it will still treat the list of drive letters as a single parameter, the comma basically eats the space.)<\/p>\n\n\n\n<p>If you want to be a bit more explicit in what you\u2019re doing, you can also pass the values in as an array:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\\Named_Parameters_Example_7.ps1 @(\"C\",\"D\",\"E\")<\/pre>\n\n\n\n<p>Note that in this case, you do have to qualify the drive letters as strings by using quotes around them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Hopefully, this article has given you some insight into the two methods of passing in variables to PowerShell scripts. This ability, combined with the ability to read JSON files in a previous article should give you a great deal of power to be able to control what your scripts do and how they operate. And now I have a script to rewrite!<\/p>\n\n\n\n<section id=\"faq\" class=\"faq-block my-5xl\">\n    <h2>FAQs: How to Use Parameters in PowerShell<\/h2>\n\n                        <h3 class=\"mt-4xl\">1. What is a parameter in PowerShell?<\/h3>\n            <div class=\"faq-answer\">\n                <p>A parameter in PowerShell lets you pass values into scripts or functions so they behave dynamically instead of using hard\u2011coded data. Parameters improve flexibility and reuse.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">2. How do I define a parameter in a PowerShell script?<\/h3>\n            <div class=\"faq-answer\">\n                <p>You define parameters with the <code data-start=\"569\" data-end=\"578\">param()<\/code> block at the top of your script, listing each variable you want to accept.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">3. What\u2019s the difference between unnamed and named parameters in PowerShell?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Unnamed parameters come from the <code data-start=\"795\" data-end=\"802\">$args<\/code> array and rely on position, while named parameters are declared in a <code data-start=\"872\" data-end=\"881\">param()<\/code> block and can be passed in any order with the <code data-start=\"928\" data-end=\"950\">\u2011parameterName value<\/code> syntax.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">4. Can PowerShell parameters have default values?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Yes &#8211; you can set default values in the <code data-start=\"1094\" data-end=\"1103\">param()<\/code> block so a parameter uses the default if no value is provided.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">5. How do I make a parameter required?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Use the <code data-start=\"1259\" data-end=\"1283\">[Parameter(Mandatory)]<\/code> attribute in the <code data-start=\"1301\" data-end=\"1310\">param()<\/code> declaration to force the user to supply a value before the script runs.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">6. Can I accept multiple values with a single parameter in PowerShell?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Yes &#8211; you can define a parameter to accept an array of values, letting users pass multiple entries at once.<\/p>\n            <\/div>\n                    <h3 class=\"mt-4xl\">7. Why should I use parameters instead of hard\u2011coding values?<\/h3>\n            <div class=\"faq-answer\">\n                <p>Using parameters makes your scripts easier to maintain, more reusable, and less error\u2011prone when moving between environments like Dev, UAT, and Prod\u2014 a principle that scales directly into cloud workflows, as shown in <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sysadmin\/powershell\/powershell-functions-reusability-restartability-azure\/\">PowerShell functions for reusability in Azure.<\/a><\/p>\n            <\/div>\n            <\/section>\n\n\n\n<section id=\"my-first-block-block_ca0f4b0a3018f85879533111f2cd0253\" class=\"my-first-block alignwide\">\n    <div class=\"bg-brand-600 text-base-white py-5xl px-4xl rounded-sm bg-gradient-to-r from-brand-600 to-brand-500 red\">\n        <div class=\"gap-4xl items-start md:items-center flex flex-col md:flex-row justify-between\">\n            <div class=\"flex-1 col-span-10 lg:col-span-7\">\n                <h3 class=\"mt-0 font-display mb-2 text-display-sm\">Subscribe to the Simple Talk newsletter<\/h3>\n                <div class=\"child:last-of-type:mb-0\">\n                                            Get selected articles, event information, podcasts and other industry content delivered straight to your inbox every two weeks.                                    <\/div>\n            <\/div>\n                                            <a href=\"https:\/\/www.red-gate.com\/simple-talk\/subscribe\/\" class=\"btn btn--secondary btn--lg\" aria-label=\"Subscribe now: Subscribe to the Simple Talk newsletter\">Subscribe now<\/a>\n                    <\/div>\n    <\/div>\n<\/section>","protected":false},"excerpt":{"rendered":"<p>Learn how to use parameters in PowerShell scripts using $args, named parameters, and the Param() block. Includes examples for passing arguments, setting defaults, and making parameters mandatory.&hellip;<\/p>\n","protected":false},"author":319367,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":true,"footnotes":""},"categories":[53,35],"tags":[159019],"coauthors":[61343],"class_list":["post-85197","post","type-post","status-publish","format-standard","hentry","category-featured","category-powershell","tag-stateofdatabaselandscapesurvey"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/85197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/319367"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=85197"}],"version-history":[{"count":18,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/85197\/revisions"}],"predecessor-version":[{"id":109307,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/85197\/revisions\/109307"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=85197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=85197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=85197"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=85197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}