PowerShell 5 and 7 command-line parameters

You can start PowerShell 5 (powershell.exe) and PowerShell 7 (pwsh.exe) and specify parameters. This blog post will show you the parameters I use most for version 5 or 7.

Shared parameters

These parameters work in PowerShell 5 and 7

Command

The -Command specifies a command that powershell .exe or pwsh.exe should execute. The command should be enclosed with curly brackets as a ScriptBlock. The & before the curly brackets indicate an invoke command. For example:

C:\Users\HarmVeenstra>pwsh.exe -Command "& {Get-ChildItem c:\scripts}"

    Directory: C:\Scripts

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           5-11-2023    16:26                Sync
-a---            1-2-2024    20:02           1542 365healthstatus.ps1
-a---           27-6-2022    12:37            100 accounts.csv
-a---           8-12-2023    13:44            194 Additional_Requirement.ps1
-a---          12-10-2022    22:43            109 hyperv.cmd
-a---           9-10-2022    19:23            882 log.txt
-a---           27-6-2022    12:39            329 Manager.ps1
-a---           14-7-2023    11:18            259 screensaver.ps1
-a---          21-10-2022    13:51           1091 Set-CorrectHyperVExternalSwitchAdapter.ps1
-a---           1-10-2023    16:39           1929 test.ps1

ExecutionPolicy

This is used the most, I guess. 🙂 With the -ExecutionPolicy, you can specify whether the prompt or script will start in AllSigned, Bypass, RemoteSigned, Restricted, Undefined, or Unrestricted mode. I usually use the Bypass option. For example:

C:\Users\HarmVeenstra>pwsh.exe -ExecutionPolicy Bypass -noprofile
PowerShell 7.4.1
PS C:\Users\HarmVeenstra>

File

The -File parameter specifies the file to execute with either powershell.exe or pwsh.exe. With pwsh.exe, it doesn’t matter where you put the parameter. With powershell.exe, the -File parameter must be specified as the last parameter because everything behind it will be interpreted as part of the script path.

The parameter is also the default parameter if no other parameters are specified. (You don’t have to specify it in that case)

Example:

C:\Users\HarmVeenstra>powershell.exe -File c:\scripts\check_ad.ps1

NonInteractive

The -NonInteractive parameter starts a PowerShell session that will terminate when encountering a prompt instead of being stuck and running forever. You want to use this in Scheduled Tasks or CI/CD pipelines. For example:

C:\Users\HarmVeenstra>pwsh.exe -NonInteractive
PowerShell 7.4.1
PS C:\Users\HarmVeenstra> Read-Host
Read-Host: PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.

Noprofile

The -NoProfile parameter skips loading your PowerShell profile. This can be useful if you have a lot of settings in there or load modules that are not needed for the script or command that you want to start using powershell.exe or pwsh.exe. Example:

C:\Users\HarmVeenstra>pwsh.exe -NoProfile
PowerShell 7.4.1
PS C:\Users\HarmVeenstra>

WindowStyle

The -WindowStyle parameter can start a PowerShell prompt in Normal, Minimized, Maximized, or Hidden view. This can be used to start a PowerShell in a different view. I use it to start a script and hide it from the user. For example:

C:\Users\HarmVeenstra>pwsh.exe -File c:\scripts\update_network_settings.ps1 -WindowStyle Hidden

Version specific parameters

PowerShell 5

PSConsoleFile

The -PSConsoleFile parameter can be used to specify a .psc1 file. This file contains all the PSSnapins that should be loaded when starting a new PowerShell prompt. First, start a PowerShell prompt, load all the PSSnapins you want, and then use Export-Console -Path C:\Scripts\Console.psc1 to save those to a file. The file contains something like this:

<?xml version="1.0" encoding="utf-8"?>
<PSConsoleFile ConsoleSchemaVersion="1.0">
  <PSVersion>5.1.26058.1000</PSVersion>
  <PSSnapIns>
    <PSSnapIn Name="Microsoft.BDD.PSSnapIn" />
  </PSSnapIns>
</PSConsoleFile>

Then you can start a new session and specify that .psc1 file:

PS C:\Users\HarmVeenstra> powershell.exe -PSConsoleFile C:\scripts\Console.psc1
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

Loading personal and system profiles took 7217ms.
PS C:\Users\HarmVeenstra> Get-PSSnapin


Name        : Microsoft.PowerShell.Diagnostics
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains Windows Eventing and Performance Counter cmdlets.

Name        : Microsoft.PowerShell.Host
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains cmdlets (such as Start-Transcript and Stop-Transcript) that are provided for use with the Windows PowerShell console host.

Name        : Microsoft.PowerShell.Core
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

Name        : Microsoft.PowerShell.Utility
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains utility cmdlets that are used to view and organize data in different ways.

Name        : Microsoft.PowerShell.Management
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell Snap-In contains management cmdlets that are used to manage Windows components.

Name        : Microsoft.PowerShell.Security
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell Snap-In contains cmdlets to manage Windows PowerShell security.

Name        : Microsoft.WSMan.Management
PSVersion   : 5.1.26058.1000
Description : This Windows PowerShell snap-in contains cmdlets (such as Get-WSManInstance and Set-WSManInstance) that are used by the Windows PowerShell host to manage WSMan operations.

Name        : Microsoft.BDD.PSSnapIn
PSVersion   : 1.0
Description : This Microsoft Deployment Toolkit 2010 snap-in contains cmdlets used to administer the contents of a deployment share.

You can see that the Microsoft.BDD.PSSnapIn is loaded now like specified in the .psc1 XML file.

PowerShell 7

ConfigurationFile

The -ConfigurationFile parameter can be used to specify a .pssc file in which certain session restrictions are configured. More information here.

NoProfileLoadTime

The -NoProfileLoadTime parameter hides the profile load time text from displaying if the load time exceeds 500 miliseconds. For example:

PS C:\Users\HarmVeenstra> pwsh.exe
PowerShell 7.4.1
Loading personal and system profiles took 923ms.

versus:

PS C:\Users\HarmVeenstra> pwsh.exe -NoProfileLoadTime
PowerShell 7.4.1
C:\Users\HarmVeenstra>

SettingsFile

The -SettingsFile parameter can be used to override the default powershell.config.json settings. You can find information about the options that you can use here. To use the file, you can run this:

PS C:\Users\HarmVeenstra> pwsh.exe -SettingsFile c:\scripts\settings.json
PowerShell 7.4.1
C:\Users\HarmVeenstra>

Wrapping up

In the chapters above I showed you the parameters that I use for my scripts and tasks that I run, you can find more information about the command-line parameters for powershell.exe (v5) and pwsh.exe (v7) in the links below:

PowerShell.exe

Pwsh.exe

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.