Create, allow and run a Windows PowerShell Script

 

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:

 

Preview PowerShell Cmdlet Help

PowerShell Cmdlet Help

created: 2022-05-02 from Bernhard

Help in a custom cmdlet is a simple comment block at the beginning of a function, or at the beginning of a PowerShell script file. ... continue reading

Preview PowerShell: Cmdlet (enhanced Function)

PowerShell: Cmdlet (enhanced Function)

created: 2022-05-02 from Bernhard

In PowerShell ISE a template for a PowerShell cmdlet can be inserted with Ctrl+J. The template extends the script with elements for documentation and thus gives the integrated PowerShell help all necessary information for the cmdlet: "get-help". ... continue reading

Preview PowerShell passing of variables (param)

PowerShell passing of variables (param)

created: 2022-05-02 from Bernhard

At the beginning of a PowerShell script parameters can be defined in the function Param(). ... continue reading

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

THANK YOU for your review!

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details