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

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 19H2 Preview Build

Introduction If you are a member of Windows 10 Insider Program you can install Windows 10 19H2 Preview Build....

How to Sign in to Windows 10 with a Microsoft Account

Introduction When you installed Windows 10 you may have created and signed in with a local account. You can...
Disable IPv6 in Windows 10

How to Disable IPv6 in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to disable IPv6 in Windows 10: Disable IPv6 from...

Advertisement

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

Advertisement

BEST OF ITECHGUIDES

active directory interview questions

35 Active Directory Interview Questions and Answers

I have compiled 35 Active Directory interview questions and answers. Where necessary, I have also provided additional information to help you understand...
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...
change password windows server 2016

How to Change Password in Windows Server 2016 (4 Methods)

Introduction This guide demos 4 ways to change password in Windows Server 2016. Options to...
powershell try catch

Powershell Try Catch Finally and Error Handling

Introduction Powershell Try Catch blocks are used to handle terminating errors in PowerShell scripting. When use Powershell Try Catch...
Configure Group Policy for Windows Updates Server 2016 (WSUS Server 2016)

How to Configure Group Policy for WSUS in Windows Server 2016

Introduction This guide demos how to configure Group Policy for Windows Updates Server 2016 (WSUS Server 2016).

RECENT POSTS

How to Enable Hyper-V in Windows 10 (3 Methods)

How to Enable Hyper-V in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Hyper-V in Windows 10. To install Hyper-V...
RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

RSAT Tools in Windows 10 Explained: Plus How to Install RSAT

Introduction Starting from October 2018 (1809) update, RSAT Tools became part of Windows 10. From this version of Windows...

How to Enable RSAT for Active Directory in Windows 10 (3 Methods)

Introduction This guide demos 3 methods to enable Active Directory in Windows 10. It is not exactly enabling "Active...
How to Install Windows 10 1909 Preview Build

How to Install Windows 10 1909 (19H2) Preview Build

Introduction Windows 10 1909 Preview is available for Windows Insiders. Made available early September, 2019 you have to be...
How to Install RSAT in Windows 10 (3 Methods)

How to Install RSAT in Windows 10 (3 Methods)

Introduction This guide demos how to Install RSAT in Windows 10. Starting with Windows 10...

How to Share a Folder in Windows 10 (3 Methods)

Introduction This guide demos how to share folder in windows 10. It covers 3 methods. Options...
Configure Map Network Drive with Group Policy

Map Network Drive in Windows 10 with Group Policy

Introduction This guide demos how to map network drive with group policy. This guide is...

How to Map Network Drive in Windows 10 (5 Methods)

Introduction This guide demos 5 methods to Map Network Drive in Windows 10. Options to...
How to Download Windows 10 ISO with Media Creation Tool

How to Download Windows 10 ISO with Media Creation Tool

Introduction This guide demos the steps to download Windows 10 ISO. You can download Windows 10 ISO with Media...

How to Install Windows 10 from Network Boot (Via WDS Server)

Introduction This guide demos how to install Windows 10 from network boot. The steps discussed in...

Advertisement

MUST READ

office 365 powershell

How to Import and Use Office 365 PowerShell Modules

Introduction Office 365 Powershell provides modules you can use to automate Office 365 tasks. But to access these modules...
mobile hotspot greyed out windows 10

Mobile Hotspot Greyed Out on Windows 10, iPhone or Android [Fixed]

Introduction If mobile hotspot is greyed out on windows 10, iPhone or Android it is likely that you do...
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...
RAID 50

RAID 50 vs RAID 10: Benefits and Disadvantages Compared

What is RAID 50 vs RAID 10? RAID 50 stripes two RAID 5 arrays while RAID 10 stripes two...

How to Fix “We Couldn’t Format the Selected Partition” Error During Windows 10 Installation

Introduction When I was writing this guide How to Install Windows 10 from USB, I suddenly received the error...

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