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>] } ]
Advertisements


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.


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

  • Was this post Helpful?
  • YesNo

LEAVE A REPLY

Please enter your comment!
Please enter your name here

FEATURED POSTS

How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Create a Business Facebook Page without a Personal Account

How to Create a Business Facebook Page without a Personal Account

This Itechguide teaches how to create a business Facebook page without a personal account. There could be a...
How to Wrap Text in Google Sheets

How to Wrap Text in Google Sheets (from a PC or the Apps)

This Itechguide teaches you how to wrap text in Google Sheets. The guide offers steps to wrap text in Google Sheets from...
How to Change Birthday on Facebook

How to Change Birthday on Facebook (from the App or PC)

This Itechguide teaches you how to change birthday on Facebook. The guide contains steps to change birthday on Facebook from the Facebook...
Best Thrillers on Netflix

The 10 Best Thrillers on Netflix

The thrillers on Netflix is quite a large number of movies. Knowing the best thrillers to watch can be a challenge.

ADVERTISEMENTS

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

Routing and Remote Access in Windows Server 2016 (Part 2): Configure NAT

Introduction This is part 2 of a 4-part "Routing and Remote Access in Windows Server 2016" series. This part...
Error Code 0xc004fc07 in Server 2016 - "We Can't activate Windows on the device right now."

How to Fix “We Can’t Activate Windows on this Device right Now”

Introduction When you try to activate Windows Server 2016 with the GUI tool you may receive error code 0xc004fc07...
windows server 2016 router configuration

How to Configure Windows Server 2016 as a Router

Introduction This guide demos Windows Server 2016 router configuration. It walks you though how to configure Windows Server 2016...
0xc000021a server 2016

4 Quick Fixes for 0xc000021a Stop Error in Windows Server 2016

Introduction There are two known causes of 0xc000021a error in Server 2016: Recently installed a...
PowerShell Not Equal

PowerShell Not Equal Operator: Applications, Examples

Introduction PowerShell Not Equal Operator is one of the Comparison Operators. PowerShell Not Equal (-ne) compares two values. If...

ADVERTISEMENTS

RECENT POSTS

How to Logout of Amazon App

How to Logout of Amazon App (from Android, iPhone or Windows 10)

This Itechguide teaches you how to logout of Amazon app. The guide offers steps to logout of Amazon app for Android, for...
How to Turn off YouTube Notifications

How to Turn off YouTube Notifications (PC or YouTube Apps)

This Itechguide teaches you how to turn off YouTube notifications. The guide offers steps to turn off YouTube notifications from a PC...
How to Change YouTube Thumbnail

How to Change YouTube Thumbnail (PC or YouTube Studio App)

This Itechguide teaches you how to change YouTube thumbnail. The guide offers steps to change YouTube thumbnail from a PC or...
How to Add Bullet Points in Google Slides

How to Add Bullet Points in Google Slides (Android or iOS Apps or PC)

This Itechguide teaches you how to add bullet points in Google Slides. The guide offers steps to add bullet points in Google...
How to Change Shipping Address on Amazon

How to Change Shipping Address on Amazon

This Itechguide teaches you how to change shipping address on Amazon. The guide offers steps to change shipping address on Amazon:

MUST READ

C:\G-Drive\Work Tools\Products Portal\1. New Business\2. Content Sites\1. iTechGuides.com\Posts\1. HOW TO\Google

How to Make a Histogram in Google Sheets (from a PC or the App)

Introduction This is a demo on how to make a histogram in Google Sheets. The guide has two sections:...

Gmail Search by Date: How to Search Gmail by Date

Introduction You can search Gmail by date for emails sent during certain time period. You could also search Gmail...
How to Cancel Spotify Premium

How to Cancel Spotify Premium

Introduction You can cancel Spotify Premium within the 30-day trial. If you cancel Spotify Premium within the trial period,...

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...

Powershell If Else Explained: Syntax and Examples

Introduction PowerShell If Else statement runs a command if a specified condition is met. The Else part of PowerShell...

LATEST DEALS

Exclusive Student Offer_Save 10% on selected Surface devices

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