Windows PowerShell is a powerful app based on the .NET framework. It relies on PowerShell commands called cmdlets. By combining them in a given order, you can perform almost any operation from the PowerShell window. To explain how it works, we have short-listed the most essential PowerShell commands.
Good to know: Windows can’t find “Powershell.exe”? Learn what to do to bring it back.
- How to Use PowerShell Commands
- Essential PowerShell Commands
- 1. Clear-Host or Cls
- 2. Convert-to-HTML
- 3. Get-Command
- 4. Get-Help
- 5. Get-Process
- 6. Get-Service
- 7. Install-Module or Install-Script
- 8. mkdir, md, rmdir
- 9. New-Item
- 10. Remove-Item
- 11. Set-ExecutionPolicy
- 12. Select-Object and Sort-Object
- 13. Start- and Stop-Process
- 14. Suspend- and Resume-Service
- 15. Test-Path
- 16. Wait-Process
- Other Essential PowerShell Commands
- Frequently Asked Questions
How to Use PowerShell Commands
To start using cmdlets, you need to launch PowerShell in Administrator mode from the search menu.
Essential PowerShell Commands
PowerShell in Windows is not only an active command-line interface but also a scripting and programming window that can run any application or process you want. While there are several hundred cmdlets, to get started, you only need to learn the most essential ones.
1. Clear-Host or Cls
Did you commit a blunder while typing, or something isn’t going as planned? Fortunately, there are do-overs in PowerShell. The handy clear-host
command can clear the entire text from the display. Just type the command on any line on your screen, and everything will vanish instantly.
You can also use the simpler cls
command, frequently used in Command Prompt, with PowerShell. These two clear text commands are handy when you encounter an error message.
To avoid typing errors in the Command Prompt or PowerShell window, copy-paste the commands for improved efficacy.
Tip: use PowerShell to hide Windows updates, as shown in this guide.
2. Convert-to-HTML
Having a big string of HTML is more aesthetic than glancing at the cluttered PowerShell screenshots. It’s also useful when you need to share script errors and other PowerShell events with non-technical team members.
All PowerShell components use .NET objects that we can view on the PowerShell screen. To have them displayed in a full browser window, use the Convert-to-HTML
cmdlet. Below is an example for viewing all PowerShell aliases on the Web. Remember to use a separate line before “Invoke-Item.”
Get-Alias | ConvertTo-Html | Out-File aliases.htm Invoke-Item aliases.htm
As soon as you execute the HTML conversion command, PowerShell will ask you to open the output file using another application. Using a browser will produce a list of objects, such as aliases.
3. Get-Command
There are primarily two types of commands: aliases and functions. PowerShell aliases serve as a nickname for a function or cmdlet. Windows has stored a few aliases by default, which you can retrieve using Get-Command
. You can also create your own aliases.
The other command type in PowerShell is functions. These use an approved verb (such as “get”) and a simple noun (such as “StorageNode”).
We are using a “Select-object” function (note the verb and noun combination) with what is known as an environment variable for computer name to display the local computer’s name.
$env:computername | Select-Object
Functions are the starting point of advanced PowerShell coding. You can use functions, such as Start-process
, with parameters and variables to create your own batch scripts, executing a series of tasks.
4. Get-Help
PowerShell has its own self-learning troubleshooting cmdlet, Get-Help
, that displays all the quick fixes and help articles in a single window. Enter the command at the end of any output for help with various modules. You may need to press Y to allow updated versions of the help content.
There are various help options via Get-help
. For example, if you want to learn what Get-process
does and what its exact syntax is, enter the following:
Get-Help Get-Process
Tip: worried about keyloggers monitoring your activity on your PC? Learn how to detect a keylogger on Windows.
5. Get-Process
Get-Process
is an essential PowerShell command that tabulates the complete list of processes on your local device or a remote computer.
For more detailed process information, you will have to specify other parameters, such as Process ID (PID) or the name of the process.
There are many specific parameters that you can include while defining the process to be found:
- List processes by specifying the file size in MB
- List processes based on priority
- Find a process’s owner
6. Get-Service
Your Windows computer has many running programs and processes. While you can view them directly on Task Manager, it is easier to view a complete list using Get-service
, which you can convert to HTML later.
Don’t remember the exact name of the service you require? You can use a wildcard symbol (*) along with the few letters you can recall.
Get-service "xyz*"
Some common flags that work in conjunction with Get-Service
include -DisplayName
, -DependentServices
, -InputObject
, and -RequiredServices
.
7. Install-Module or Install-Script
PowerShell is one of the safest options to install software packages for Windows software, such as Microsoft Teams, McAfee Security, Bing Translator, the latest Xbox games, etc. To get a complete list of software supported in PowerShell for all users, enter:
Get-AppxPackage -AllUsers
A better way is to search for your desired package in PowerShell Gallery, a single-stop resource for the latest software packages. Once you find it, enter a command, such as Install-module
, Install-Script
, or Install-Package
. The following shows the installation of the Minesweeper game.
Install-Script -Name Minesweeper
Wait a few seconds or minutes for the package to install through your PowerShell. It will be available later in Windows.
Another useful command is Find-package
, which searches for a software package on your computer or the Web.
Tip: you can also use the Window Package Manager to install and update programs.
8. mkdir, md, rmdir
mkdir
is not a native PowerShell command. It is, however, a widely used alias of new-item
to create directories, as this syntax is very popular in DOS and Linux. When you use mkdir
with a name of your choice, it creates an empty folder.
mkdir "name of your empty folder."
Make PowerShell point to your newly created folder using a simple cd
command.
cd "name of the newly created folder."
The newly created folder is always located along the path C:\Windows\System32
, but you could use cd
to point to a different folder.
You can substitute mkdir
with md
for the same purpose as above. To remove any directory content, use a rmdir
(remove directory) command:
rmdir "name of the folder with empty content."
9. New-Item
Unlike aliases such as mkdir
and md
, New-item
is the official cmdlet used in PowerShell to create new items and set their values. On top of creating files and folders, you can use it to make registry keys and entries.
- To create a new directory using the
New-item
command, type:
New-item -Path "Directory path location\" -Name "Name of your choice" -Item Type "directory"
- As soon as a directory is created, you can place new files with
New-item
inside that folder.
New-item -path "Created directory path location\" -Name "File name.txt" -ItemType "file" -Value "insert any text of your choice."
- The newly created file can be found in its destination folder.
10. Remove-Item
Do you want to remove all files with an extension, such as .TXT, .DOC, .PDF, .WMV, from a folder? First, point to the exact folder path using cd
, then use the cmdlet Remove-Item
as shown below:
Remove-Item * -Include *(filetype) - Exclude *(any variable or number)"
The above method is very useful to identify and remove any hidden or read-only files or delete any files with special characters.
Good to know: having trouble removing a file from your PC? Learn how to force delete un-deletable files in Windows.
11. Set-ExecutionPolicy
For security purposes, PowerShell has its own execution policy that affects configuration files, scripts, and other parameters. This safety feature can be executed using the Set-executionPolicy
command, along with a flag, such as -force
, which overwrites existing items. It follows the syntax given below.
Set-ExecutionPolicy<br>[-ExecutionPolicy]<br>[[-Scope] ]<br>[-Force]<br>[-WhatIf]<br>[-Confirm]<br>[]
For example, using the Set-ExecutionPolicy
command to install Chocolatey software looks like the code shown below. Once executed, it will download and install the official Chocolatey software on your computer.
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
12. Select-Object and Sort-Object
If your folder or subfolder has many files and extensions, you may want to sort or select them in ascending or descending order based on a predefined property. This is where the cmdlets Sort-Object
and Select-Object
can help you rearrange multiple objects in one go.
The sorting and selection can be applied to any files in different folders as well as the existing processes on your computer. Below is an example of sorting and selecting the ten most important processes based on working set (WS) size in megabytes.
Get-process | Sort-Object -Property WS | Select-Object - Last "Number of items"
You can use sorting and selection on various other parameters, such as historical information of various files, sorting all the list items contained inside a Notepad file, and sorting various events on your computer.
13. Start- and Stop-Process
Whether you want to start a new process in Windows or stop an existing process, you can quickly use PowerShell cmdlets to achieve that goal.
To quickly terminate a process, use the Stop-Process
cmdlet. The following example shows how to close a process for WordPad.
Stop-process -Name "Process or Application name"
Likewise, to start a new application directly from the PowerShell window, enter the Start-process
cmdlet and -FilePath
flag.
Start-Process -FilePath "Application name"
Tip: learn how to stop background apps and processes from running in Windows and eating up your resources.
14. Suspend- and Resume-Service
Instead of starting or stopping a running service, you can suspend it directly. The Suspend-service
cmdlet is simple to use and helps you reduce the RAM and CPU usage of services you aren’t currently using.
Note: you can use Get-service
to get the name of the service to be suspended.
Suspend-service -DisplayName "Service full name"
Any terminated service can be resumed later with a simple Resume-service
command. If you’re not sure which services you can suspend without affecting your system negatively, use the following command:
Get-Service | Where-Object {$_.CanPauseAndContinue -eq "True"} | Suspend-Service -Confirm
15. Test-Path
If you want to avoid making file path errors in your programming, you need to be sure that the file path in syntax is accurate. While you can always verify with File Explorer, a more accurate way to confirm this is by using the Test-path
cmdlet, which simply answers “True” or “False” whether a given path exists.
Test-Path -Path "Path name"
You can modify the command to test the availability of a given file, check whether the registry path is accurate, and determine whether there are other files besides a specific file type on your computer.
16. Wait-Process
When multiple processes are running in tandem, you may want to wait for one or more of them to be stopped before they can be used again. The Wait-Process
cmdlet can help greatly. You can specify a timeout for which PowerShell will wait before the process is stopped.
Wait-Process -Name "Process name, -Timeout (in seconds)
Other Essential PowerShell Commands
Apart from the PowerShell commands listed above, it’s also helpful to learn the following cmdlets and functions:
Echo
is a cmdlet used to print any value on the PowerShell consoleGet-Host
gives full details of the program hosting PowerShell in WindowsTest-JSON
is an advanced cmdlet used to test whether a string is a valid JSO objectTrace-command
traces the entire expression or command to find the parameters and flags it containsWrite-output
, as the name suggests, gives an output of the last command in the console window.
Tip: get up to speed with these legitimate Windows processes that can look like malware to avoid removing them from your system.
Frequently Asked Questions
How can I solve the "PowerShell script not recognized as the name of a cmdlet" error?
If PowerShell is not recognizing the name of a cmdlet, it might be due to the Path variable not being set correctly. Fix any path variable error by correcting its invalid folder path.
How do I capture error output as a variable in PowerShell?
PowerShell has an error variable parameter, $ErrorVariable
, which is designed to capture all error outputs when you enter the various cmdlets. These can be used for troubleshooting.
Image credit: DepositPhotos. All screenshots by Sayak Boral.
Our latest tutorials delivered straight to your inbox