How to pass parameters to a Windows PowerShell script

Basics

When creating scripts with Windows PowerShell, one of the most common features is pass parameters to a Windows PowerShell script

Just as this is necessary in batch scripting, it is also regularly required in PowerShell scripts. One example would be if you want to give the script the transfer path for a backup location. Exactly this configuration describes the tutorial.

Creating parameters in the script

Firstly, it should be clarified how to create variables in Windows PowerShell. Generally, you can specify a variable with its name followed by its content. But if you want to pass variables to a script, you have to specify them in the declaration part under param().

Only then they can be passed to the script. But as already mentioned, the variables can also be filled with content in the param part. This is accurately what the following example shows. Here, the two variables (variable1 and variable2) are assigned two values.

parma (
$variable1 = "Hallo ",
$variable2 = "Markus "
)
Write-Host $variable1,$variable2
pass parameters to a Windows PowerShell script

When you run this script, it outputs the contents of the two variables. The output is generated by the Write-Host cmdlet.

Calling PowerShell script with parameters


As already explained above, the variables must be declared within param () in Windows PowerShell. To call the script, you now only have to give the script the contents of the variables. The call looks like this:

.\01-variable-uebergabe.ps1 "Servus","Hans" Servus Hans

Windows PowerShell Skript Mit Parameter Aufrufen pass parameters to a Windows PowerShell script

With this option, it is easy to pass parameters to a PowerShell script now. If you have multiple parameters, they must be separated by commas. Just like in the example above.

If you would like to learn more about Windows PowerShell, you can always find excellent and detailed courses on Udemy. We recommend the courses of Patrick Grünauer: The beginner’s course is the following: Automation with PowerShell (1/3): Basics*.

defeca6696b84618b70d3fc2bc7af3ab
Der Blogbetreiber und Autor: Markus Elsberger

Über den Autor

Mein Name ist Markus Elsberger und ich beschäftige mich mit der Administration von Windows und Linux Systemen sowie mit diversen Themen bzgl. Netzwerktechnik. In meiner Lehrtätigkeit erstelle ich verschiedene Szenarien und teste auch verschiedene Konfigurationen, welche ich in diesem Blog festhalten möchte.


Kostenlose Befehlsreferenzen und E-Books
(Netzwerkanalyse, Benutzerverwaltung, Festplattenkonfiguration, u. v. m.)


Nach oben scrollen
×