Powershell parameter

how do i use the parameter function? lets say for example i want to create a function that will ping a host. i then want to use the parameter function to specify a hostname.

ping.ps1 mycomputer
Kylo RenSystem EngineerAsked:
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.

you might try using the $args[0] variable.   It might be 1 instead of 0.  Here is some more info:

Kylo RenSystem EngineerAuthor Commented:
thanks, i found that link before but it is a little confusing. was hoping someone has a easier example
Do you have any more of your code available to show how you are using the variable. you should be able to just add the  arg to the ping request..

ping code + $args[0]

I think 0 is the first parameter.  In some languages it is the name of the script, so it might be 1 instead.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Dale HarrisProfessional Services EngineerCommented:
Try this easy script:

param (
if ($HostIP -ne "")
"Host IP = $HostIP"
$HostIP = Read-Host "Please enter in the IP of the host you want to ping"

Ping $HostIP

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
Dale HarrisProfessional Services EngineerCommented:

.\ScriptName ""


Dale Harris
Kylo RenSystem EngineerAuthor Commented:
dale thats exactly what i was looking for.

is this the line that creates the pop up?

param (

Also, what does this line do?

"Host IP = $HostIP"

do you need the last line since i will be passing the host ip through the script? ex. .\script ip
Ping $HostIP

sorry about the extra questions. i want to make sure i understand it though

Dale HarrisProfessional Services EngineerCommented:
The script works like the following:

param (
#Whatever you put in here will be your parameter information, no prompts for information

So if the value of $HostIP actually is something and not "" or blank, it will show you what it is:
"Host IP = $HostIP" and it will show up as "Host IP = <whatever you put as the parameter>"

You do need the last line because that's the actual code to ping the host you're putting through the parameter.

It's pretty much saying

Ping google.com

if google.com was your parameter

I enjoy explaining this to you and I think Powershell is very much worth your time to learn.

Chris DentPowerShell DeveloperCommented:
This is a more extensive example, the param block is exceptionally powerful.
Function Start-Ping {
  # Optional: Extended behaviour can be enabled in PowerShell 2 with CmdLetBinding
  # [CmdLetBinding( <Parameters> )]
  # e.g. [CmdLetBinding( SupportsShouldProcess = $True )]
  # Start of parameter definition

    # Parameter properties, such as mandatory and position and so on
    [Parameter( Mandatory = $True, ValueFromPipeline = $True )]
    # Parameters can be validated using a number of different methods. For example, make sure
    # it has a value
    # Parameters can be forced to a specific type. For example, String
    # Then a parameter name is defined and a default value can be assigned (optionally)
    [String]$HostName = $Env:ComputerName

    # Repeat for any other parameters (a comma separated list

  Process {
    ping $HostName -n 1

# This will happily continue, pinging the local machine ($Env:ComputerName is an 
# environmental variable)

# This will tell you off for passing a blank value
Start-Ping ""

# These two are basic usage
Start-Ping localhost
Start-Ping -HostName localhost

# This is the pipeline part (ValueFromPipeline = $True)
"localhost" | Start-Ping

# Or a list from the pipeline (permissible because we used Process { })
"localhost", "someremotehost" | Start-Ping

Open in new window

Fun, huh?

Kylo RenSystem EngineerAuthor Commented:
Dale, Thanks for the explanation.  I am now comfortable with parameters! thanks again
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.