PowerShell is one most powerful scripting tools that Microsoft has created. In this post, we will share what is PWSH.exe, and list of important PWSH syntax. I am sure many of use have used Windows PowerShell, but PWSH is now a cross-platform scripting tool that works on Windows, macOS, and Linux. However, it is not supported on WSL i.e. Windows Subsystem on Linux, and attempting to set PWSH as the login shell will lead to unstable WSL.
What is PWSH.EXE?
Before we start, let’s clear out one detail. PWSH.EXE is the new name for PowerShell. Since version 6 it is called PowerShell Core. It was earlier named as powershell.exe which you must have seen installed in Windows (version 5.1). No wonder every time you launch PowerShell in Windows, you get a message:
“Try the new cross-platform PowerShell https://aka.ms/pscore6.”
You can read about the difference between PowerShell and PowerShell Core.
Fast forward today, PowerShell has reached version 7 which is a major change compared to version 6 and uses .NET Core 3 instead of the .Net Framework. If you want to give it a try, learn how to install PowerShell 7.0 on Windows 10.
Important PWSH Syntax
-File | -f: If you have commands into a script file, you can use it as an input. You can also arguments for the files when applicable.
pwsh -File .\test.ps1 -TestParam $env:windir
-Command | -c: Use this to execute a command or ScriptBlock. ScriptBlock is set of functions enclosed withing {}
pwsh -Command {Get-WinEvent -LogName security}
or
@' "in" "hi" | % { "$_ there" } "out" '@ | powershell -NoProfile -Command -
-EncodedCommand | -e | -ec: Use this when there is a need to use complex quotation marks or curly braces.
$command = 'dir "c:\program files" ' $bytes = [System.Text.Encoding]::Unicode.GetBytes($command) $encodedCommand = [Convert]::ToBase64String($bytes) pwsh -encodedcommand $encodedCommand
-Login | -l: On Linux and macOS, starts PowerShell as a login shell, using /bin/sh to execute login profiles such as /etc/profile and ~/.profile. It is not applicable to Windows.
You will have to verify the absolute path listed under /etc/shells. You can use the chsh utility to set your current user’s shell to pwsh.
chsh -s /usr/bin/pwsh
-SettingsFile | -settings
If you want to overwrite the global settings with local project settings, then you can specify the settings file using this option. The system-wide settings are available in powershell.config.json.
pwsh -SettingsFile c:\myproject\powershell.config.json
Complete List of PWSH Syntax
pwsh[.exe] [[-File] <filePath> [args]] [-Command { - | <script-block> [-args <arg-array>] | <string> [<CommandParameters>] } ] [-ConfigurationName <string>] [-CustomPipeName <string>] [-EncodedCommand <Base64EncodedCommand>] [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}] [-Interactive] [-Login] [-MTA] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile] [-OutputFormat {Text | XML}] [-SettingsFile <SettingsFilePath>] [-STA] [-Version] [-WindowStyle <style>] [-WorkingDirectory <directoryPath>] pwsh[.exe] -h | -Help | -? | /?
If you need more details, you can visit docs.microsoft.com.