Powershell.exe Command: Syntax, Parameters and Examples

-

|

Introduction

You may be wondering why write on Powershell.exe Command. Are there special commands for Powershell.exe? Yes! When you begin to script with Powershell you quickly realize that you may need to start Powershell with different options.

This guide covers the general Syntax of the Powershell.exe Command, explains its parameters and conclude with some examples.

Powershell.exe Command Syntax

The syntax of Powershell.exe Command is:

PowerShell[.exe] 
[-EncodedCommand ]
[-ExecutionPolicy ]
[-InputFormat {Text | XML}]
[-Mta]
[-NoExit]
[-NoLogo]
[-NonInteractive]
[-NoProfile]
[-OutputFormat {Text | XML}]
[-PSConsoleFile ]
[ -Version <Windows PowerShell version> ]
[-Sta]
[-WindowStyle ]<style>
[-File <FilePath> [<Args>]]
[-Command { - | <script-block> [-args <arg-array> ] | <string> [<CommandParameters>] } ]
Advertisement

Powershell.exe Command Parameters

This section explains the parameters of Powershell.exe Command Syntax. See the table below:

S/NParameterMeaning
1-EncodedCommand <Base64EncodedCommand>Accepts a base-64-encoded string version of a command. This is useful when you need to submit Powershell commands with complex quotation marks or curly braces
2-ExecutionPolicy <ExecutionPolicy>This parameter sets the default execution policy for the current session. The execution policy is saved in the $env:PSExecutionPolicyPreference. This does not modify the execution policy set in the registry.
3-File <FilePath> [<Parameters>]Specifies a script to run. It accepts the scripts parameters.
4-InputFormat {Text | XML}Describes the format of data sent to PowerShell. Valid values are “Text” (text strings) or “XML”
5-MtaStarts PowerShell using a multi-threaded apartment. Multi-threaded apartment (MTA) is the default for PowerShell 2.0. The default in PowerShell 3.0 is single-threaded apartment (STA)
6-StaStarts Windows PowerShell using a single-threaded apartment
7-NoExitIf specified, PowerShell will not exit after execution
8-NoLogoHides the Copyright information that normally displays when PowerShell starts.
9-NonInteractiveExecutes without presenting an interactive prompt to the user
10-NoProfileStarts without loading the Windows PowerShell profile
11-OutputFormatSets the formatting of the output from Windows PowerShell. Valid values are “Text” or “XML”
12-PSConsoleFile <FilePath>Loads the Windows PowerShell console file specified in <FilePath>
13-Version <Windows PowerShell Version>Starts Windows PowerShell with the specified version. Valid values are 2.0 and 3.0.
14-WindowStyle <Window Style>Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.
15-CommandExecutes the command specified. The value of Command can be “-“, a string. or a script block. Script blocks must be enclosed in braces ({})
16-Help, -?, /?Displays help for PowerShell.exe. You can use PowerShell.exe -Help, PowerShell.exe -? or PowerShell.exe /?

Powershell.exe Command Examples

In this section I will give some Powershell.exe Command examples.

To start Powershell in version 2.0 and not display the copyright information, use the command below:

PowerShell -version 2.0 -NoLogo
 Powershell.exe Command Examples

The result of the last command (shown above), may not mean anything until you start PowerShell.exe without the -NoLogo parameter.

PowerShell -version 2.0

In the previous image, the above command displays the Windows Copyright information. This is because I did not use the -NoLogo parameter.

In another example, if you want to specify the input and output format, use the command below:

PowerShell -InputFormat text -OutputFormat XML

Here is the result in PowerShell:

powershell.exe command

Since I specified input format as text, I can enter text into powershell.exe command. If you press Enter, PowerShell will return to its prompt.

However, if you specify InputFormat as XML and OutputFormat as text, PowerShell command behaves differently.

PowerShell -InputFormat XML -OutputFormat text

See the result of the above command below:

powershell.exe command

PowerShell.exe -Command Parameter Examples

The -Command parameter is the one of the most versatile PowerShell Parameters. The reason is simple. You can run other PowerShell commands with this parameter. You can also run scripts.

The practical application of these these two scenarios will be demonstrated in the next section. In this section, I will demonstrate how to use PowerShell.exe -Command Parameter.

In my first example, I will show how you can run Get-EventLog with the -Command Parameter.

Here is the command:

PowerShell -Command {Get-EventLog -LogName security}
To run the previous command, you MUST open PowerShell as Administrator

Sometimes you may need to write a string that executes a command. In this instance you will need to use an ampersand operator, & followed by the command as shown below:

PowerShell -Command "& {Get-EventLog -LogName security}"
I have had instances where I had to use the ampersand operator, & before a script can run. This may be required when you schedule a PowerShell script with Windows task scheduler.

PowerShell.exe -EncodedCommand Parameter Examples

As I mentioned in the Parameter section of this guide, the -EncodedCommand parameter is used to specify the Base64encoded string version of a command.

In this example, I will show how to run the command, ‘Get-Childitem “c:\program files”‘ in base-64-encoded string.

To convert the command to a base-64-encoded string, use the steps below:

  • Set a variable for the command:
$normalcommand = 'Get-Childitem "c:\program files" '
  • Next, convert the command into bytes as shown below:
$bytes = [System.Text.Encoding]::Unicode.GetBytes($normalcommand)
  • Then convert the bytes to Base64String:
$encodedCommand = [Convert]::ToBase64String($bytes)
  • Finally, run the the command in Base64String:
powershell.exe -encodedCommand $encodedCommand

Here are the results of the commands in PowerShell:

PowerShell.exe -EncodedCommand Parameter Examples

To see what the Base64String string looks like, run the command below:

 $encodedCommand 

This is how it looks like in PowerShell.

Advertisement

How to Schedule Powershell Script to Run in Task Scheduler

In the last part of this guide I will demonstrate how to schedule a PowerShell script with task scheduler.

In this example, I will schedule Get-FreeDiskSpace script. This script will run weekly and send you a report of free disk spaces on all servers.

Follow the steps below to schedule Get-FreeDiskSpace PowerShell script:

  • Open PowerShell or command prompt. Then type this command and press Enter.
taskschd.msc

This will open Task Scheduler.

How to Schedule Powershell Script to Run in Task Scheduler
  • Right-click Task Scheduler and select Create Task.
How to Schedule Powershell Script to Run in Task Scheduler - create scheduled task
  • Give the task a name. You could also enter a description. Then click Triggers tab.
  • To create a new trigger click New.
A trigger is what makes your task run in Task Scheduler.
  • On the New Trigger page, on the Begin the task drop-down ensure that On a schedule is selected. Then select the frequency. If you select Weekly, then check the box beside the day of the week you want your task to run. Finally, configure Advanced settings. Click Ok. Then, on the Create Task page, click Ok again (Second image below).
  • Next, click the Actions tab.
This is where you enter your PowerShell.exe command.
  • On the Actions tab, click New. On the New Action page, beneath program/script, enter the command below:
PowerShell 
  • Then beside Add arguments (optional), enter the following
-File C:\PS\Get-FreeDiskSpace.ps1
Your script block must be enclosed in braces ({}) as shown above. You must specify the full path to your script. The path must include the extension .ps1
  • When you finish, click Ok.

Conclusion

PowerShell.exe command is for advanced PowerShell users. I hope I have simplified how to use the various parameters.

If you have any question or comment use the “Leave a Reply” form at the end of the page. You could also share your experience with PowerShell commands.

Other Helpful Guides

Additional Resources and References

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

network discovery keeps turning off server 2016

How to Fix Network Discovery If it Keeps Turning Off in Server 2016

Does network discovery keep turning off in your Windows server 2016? It is likely that one of its dependent services is not...
how to install windows 10 1903 update manually

How to Install Windows 10 1903 Update Manually

Windows 10 1903 Update was released in May, 2019. But some users are not yet offered the update via automatic update. The...
spotify web player not working

Spotify Web Player Not Working [Fixed]

Introduction Spotify Web Player may stop working for you with the following error messages: "Spotify Web Player an Error...
DISM.exe /Online /Cleanup-Image /Restorehealth

DISM.exe /Online /Cleanup-Image /Restorehealth Explained

What is DISM.EXE /Online /Cleanup-image /RestoreHealth? "DISM.exe /Online /Cleanup-Image /Restorehealth" is a DISM command that repairs issue with the...
DHCP Relay agent

DHCP Relay Agent: Configuration in Windows Server 2016

What is a DHCP Relay Agent? A DHCP Relay Agent allows DHCP clients in a different network subnet to...

TRENDING POSTS

Remote Desktop Connection

Remote Desktop Connection an Internal Error Has Occurred [Fixed]

Introduction I recently received the error message "Remote Desktop Connection an Internal Error Has Occurred". It was strange because...

Find My Samsung: Register and Use Samsung Find my Mobile

Introduction Ever wondered how you could find your Samsung phone if you lost it? Find my Samsung or Samsung...
What is the Difference Between PowerShell and CMD?

Windows Powershell vs CMD: Differences and Similarities

Introduction This short guide compares Windows PowerShell vs CMD (Windows command prompt). I will cover the history and nature...
Spotify No Longer Supports this Version of Microsoft Edge

Spotify No Longer Supports this Version of Microsoft Edge [Fixed]

Introduction When you open Spotify web player on Microsoft Edge, you may receive the error message "Spotify No Longer...
Windows 10 Won't Boot

Windows 10 Won’t Boot With Black Screen? 3 Ways to Fix It

Why Won't Windows 10 Boot Up? If your Windows 10 stops with a black screen, the first question in...

BEST OF ITECHGUIDES

DISM.exe /Online /Cleanup-Image /Restorehealth

DISM.exe /Online /Cleanup-Image /Restorehealth Explained

What is DISM.EXE /Online /Cleanup-image /RestoreHealth? "DISM.exe /Online /Cleanup-Image /Restorehealth" is a DISM command that repairs issue with the...
Walmart Baby Registry

Walmart Baby Registry: Your Definitive Guide

Introduction Walmart Baby Registry gives expectant mothers a place to create a list of items they need for their...
powershell executionpolicy

PowerShell ExecutionPolicy Explained

What is PowerShell ExecutionPolicy? ExecutionPolicy in PowerShell is a security feature that controls how PowerShell loads configuration files and...
Active Directory Domain Services

Active Directory Domain Services: Installation & Configuration

What is Active Directory Domain Services? Active Directory Domain Services (AD DS) is at the core of Microsoft's Directory...
How-to-Calculate-Standard-Deviation-in-Excel

How to Calculate Standard Deviation in Excel

Introduction Standard Deviation is a measure of how dispersed values are from their mean (average). You can calculate standard...

RECENT POSTS

disable cortana windows 10 featured

How to Disable Cortana in Windows 10 (2 Methods)

Introduction Some Windows 10 users may not like Cortana. Solution? Disable Cortana. You are probably reading this because you...
bootrec /fixboot access is denied

How to Fix BootRec /FixBoot Access is Denied Error in Windows 10

Introduction BootRec /FixBoot Access is Denied Error in Windows 10? This error is likely caused by corrupt EFI directory.
Reset Windows 10

How to Reset Windows 10 (2 Methods)

Introduction If your Windows 10 is broken, one available way to fix it is to use Windows 10 reset....
system restore windows 10

How to Enable and Use System Restore in Windows 10

Introduction System restore in windows 10 is a very important tool that is probably ignored by most users. But...
windows has stopped this device code 43

How to Fix Windows Has Stopped this Device Code 43 Error

Introduction Sometimes you may receive "Windows has stopped this device code 43" error. This error is likely to come...
Windows could not automatically detect this network's proxy settings

How to Fix “Windows Could not Automatically Detect this Network’s Proxy Settings”

Introduction You suddenly lose the ability to connect to the internet. Then you run network troubleshooter and it returns...
printer offline

5 Ways to Restore Your Printer Online If Status is Offline

Introduction Is your printer offline? Setting it online is very simple but sometimes it is more than just setting...

How to Fix “BootMgr is Missing” Error in Windows 10

Introduction If you receive Fix "BootMgr is missing" Error in Windows 10, the default response is panic! But you...
windows 10 search featured

How to Fix Windows 10 Search If it Stops Working

Introduction Are you having troubles with Windows 10 search? Apparently, it is a fairly common problem with a number...
oooops, something went wrong. reload

How to Fix “Oooops something went wrong. reload” Spotify Error

Introduction If you receive "Oooops something went wrong. reload" Spotify error, do not panic. The fix is simpler than...

MUST READ

powershell replace

PowerShell Replace “Method” and “Operator”: Syntax, Examples

Introduction PowerShell Replace can be used to replace character strings, texts or special characters. PowerShell has a Replace...
spotify no longer supports this version of chrome

How to Fix “Spotify no Longer Supports this Version of Chrome”

Introduction Have you been frustrated by "Spotify no Longer Supports this Version of Chrome" error? You are not alone!...
Active Directory FSMO Roles

Active Directory FSMO Roles Explained

Introduction This article offers a simplified explanation of the 5 Active Directory FSMO (pronounced "FisMO") roles.
Check Disk (CHKDSK) Windows 10

Check Disk (CHKDSK) in Windows 10: Syntax, Parameters, Examples

Introduction Check Disk (CHKDSK) is a Windows 10 command tool that checks a disk for errors, fixes errors, recovers...
How to Change Profile Picture on Spotify

How to Change Profile Picture on Spotify (from Desktop or Mobile)

Introduction You can change profile picture on Spotify from the desktop or mobile app. This guide covers the steps...

By using this website you agree to accept our Privacy Policy and Terms & Conditions