Create and run a Windows PowerShell Script

Diese Seite gibt es auch in Deutsch

In the simplest case, a PowerShell script is a text file containing a series of PowerShell commands. A PowerShell script is understood to be a file with the extension .ps1. The script file can contain a collection of commands, functions or cmdlets.

PowerShell can be used to automate almost anything, from simple procedures to complex program flows. In addition to the classic PowerShell console, there is also the PowerShell development environment (PowerShell ISE) for scripting, this is basically a text editor optimized for PowerShell with an integrated console. The development environment can easily be called via the Windows search dialog:

Starting the PowerShell development environment (ISE).

The upper half can be expanded by clicking on "Script", "Show Script Pane", it contains the source code, the lower half shows the PowerShell command line and in the right pane PowerShell commands can be searched and inserted:

our first script

Our first script should simply output "hello":

the following PowerShell command is used for this:

Write-Host "hello". So the command will be written to the script area. When starting the script (green arrow key), the script will be started in the command line.

Execution policy

Execution policy protects the computer from running untrusted scripts.

Depending on the execution policy setting, which varies depending on the operating system version, after saving the script to a file, it may not be able to run.

The setting defaults to "restricted" in Windows, which prevents scripts from running, the following error is displayed:

PS C:\Windows\system32> C:\Users\LiBe\Documents\savedScript.ps1
File C:\Users\LiBe\Documents\savedScript.ps1 cannot be loaded because running 
scripts is disabled on this system. For more information, see 
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordEx 
   ception
    + FullyQualifiedErrorId : UnauthorizedAccess

Allow PowerShell scripts

The PowerShell execution policy prevents PowerShell scripts from running when set to Restricted.

To view the execution policy and adjust it later, I start PowerShell as administrator:

Using get-executionpolicy -list the current setting can be displayed:

Using the set-executionpolicy command, the setting can be adjusted.

As an example I allow local scripts or signed scripts from the internet with the policy "RemoteSigned".

The complete command is then: Set-ExecutionPolicy remotesigned

Set-ExecutionPolicy unrestricted would allow all PowerShell scripts. This setting should not be used for security reasons, because insecure scripts from the internet would also work. So "remotesigned" is recommended. Unknown scripts can be allowed in the Explorer with a right click: "Properties" and "Unblock".

Alternatively, files can be allowed with the PowerShell command Unblock-File:

with Unblock-File *\* also multiple files can be allowed.

Zone identifier

The difference between a locally created script and one copied from the Internet is the zone identifier.

In order for the Internet script to run, this must be allowed in advance:

If a PowerShell script is copied from the Internet, the file gets the following zone identifier:

Add-Content -Path 'Script.ps1' -Value "[ZoneTransfer]`nZoneId=3" -Stream 'Zone.Identifier'.

Clicking "Allow" cleared the zone zone identifier, analogous to the following command

Clear-Content -Path 'Script.ps1' -Stream 'Zone.Identifier'

Bypass

Finished scripts can be started later without adjusting the ExecutionPolicy. To do this, the file is called from the command prompt via PowerShell.exe with the flag: Bypass:

PowerShell.exe -ExecutionPolicy Bypass -File .unknown.ps1 

Instead of the file, a command can also be passed:

Powershell -command "Write-Host 'Hello'"

multiple commands can be separated with a semicolon:

Powershell -command "Write-Host 'Hello'; Write-Host 'you'"

It would even be possible to have the following example: a .cmd file loads an external script file and starts a function contained in it with certain parameters.

PowerShell.exe -ExecutionPolicy Bypass -command ". \sharemyFunctions.ps1; one-of-the-functions-in-myFunctions -paramteter1 1 -parameter2 2"

For details about myFunctions.ps1, see: PowerShell Passing Variables (Param)

Running the script

To run the script from PowerShell, the full path must be specified. If the PowerShell console is already in the script path, the script can be started with .\ in front of the filename.

As an example I create the following PowerShell script under c:\temp\test.ps1

Content of the text file:

Write-Host "hello

Since PowerShell is also about text files, they can also be created in the editor.

The call from the full path then looks like this:

The call from the path c:\temp:

 

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!


Top articles in this section

PowerShell Log-Files: Logging into a textfile - write to file

PowerShell Log-Files: Logging into a textfile - write to file

created: 05.05.2022 from Bernhard

Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript. ... continue reading

Preview PowerShell text file and csv read / write

PowerShell text file and csv read / write

created: 02.05.2022 from Bernhard

PowerShell text file and csv read / write ... continue reading

PowerShell Loops and Array

PowerShell Loops and Array

created: 02.05.2022 from Bernhard

An array stores multiple values, similar to a 2-column table. ... continue reading


Questions / Comments