How to create powershell script I can pass a parameter to

I would like to write a script such that I can pass a variable by the command line to the script. Example would be like below.

.\script.ps1 computername

Open in new window

Usually in my scripts I would actually enter my variables within the script itself.  A very simple example would be

get-adcomputer computername

Open in new window

What would I need do to to allow me to pass a parameter to the script through the command line and have it return a value?
LVL 35
Joseph DalyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Justin YeungSenior Systems EngineerCommented:
change your script as a function.

function Verb-Noun #example get-something
        # Param1 help description


{your script}

Joseph DalyAuthor Commented:
Im sure that would work but would you mind explaining what exactly that does and how it does it. Looking at the code I am pretty over my head.
Justin YeungSenior Systems EngineerCommented:
so what happen is, instead of set the variable within the script, you can specific it as a "required parameter" with (Mandatory=$true).

when you run the script, it will come up and ask you what is $computername will be by typing it in.

powershell can convert script that run to a function or module that can be used with different variable setup.

here is the full powershell 3.0 tutorial from Microsoft Academy, which explain and you can learn from it step by step to powershell.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Justin YeungSenior Systems EngineerCommented:
This course actually explain it really well, and I am sure you can learn from it.
Joseph DalyAuthor Commented:
How would I create a function so that I could pass a .\name.ps1 parameter?
Justin YeungSenior Systems EngineerCommented:
sorry I don't understand your question.....
Don't worry about cmdlet binding and other advanced functions. Just stick to basic named parameters until you get your head around it all. If you want to pass a parameter to powershell you can do it named, or unnamed. An unnamed parameter is position (taken in the order it is given).

## Top of the script
$ComputerName = $args[0]

Open in new window

This would say take:

.\script.ps1 computer1

$ComputerName would be assigned the value computer1. If you were to do:

.\script.ps1 computer1 computer2

You would need to assign both values with $args[0] and $args[1]. $args is an array of all the arguments you provide to a script or function. You can see this is not ideal. It relies on the arguments in the right position always. This is where a named parameter comes in.

## Top of the script
param (

Open in new window

This will let you launch your script with a named parameter like so:

.\script.ps1 -ComputerName Computer1

Casting as a string "[string]" is not required, but its good practice.

If you want to get a bit more advanced, cast it as a string with accepts multiple values.

## Top of the script
param (
Foreach ($Computer in $ComputerName)

Open in new window

This will let you launch your script with multiple computer names specified:

.\script.ps1 -ComputerName Computer1, Computer2, Computer3, Computer4

If you want to know more about what Justin Yeung is talking about after understanding parameters better look into "Powershell Advanced Functions" on Google. There are a number of good articles. Few Powershell userse will have a need to used Advanced Functions. I recommend getting a better understanding of named and unnamed parameters for scripts and functions first.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joseph DalyAuthor Commented:
Thank you the explanation really helped.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.