Link to home
Start Free TrialLog in
Avatar of Joseph Daly
Joseph DalyFlag for United States of America

asked on

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?
SOLUTION
Avatar of Justin Yeung
Justin Yeung
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Joseph Daly

ASKER

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.
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.

http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start
This course actually explain it really well, and I am sure you can learn from it.
How would I create a function so that I could pass a .\name.ps1 parameter?
sorry I don't understand your question.....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you the explanation really helped.