Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
منوعات تقنية

16 Essential PowerShell Commands to Know

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

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.

Clear host command to remove entire text in PowerShell window.

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.

Entering cls command below error message to remove the entire text in PowerShell.

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
Convert-to-HTML command in PowerShell to display aliases in a web page.

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.

A complete list of PowerShell aliases viewable on a web browser.

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.

List of aliases under Get command in PowerShell.

The other command type in PowerShell is functions. These use an approved verb (such as “get”) and a simple noun (such as “StorageNode”).

List of functions displayed under Get-command in Powershell.

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
Getting computer name using Select-Object function and environment variable in PowerShell.

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.

PowerShell updating help for various modules after Get-Help command.

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:

Various examples displayed in PowerShell window for get-help command.

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.

Get-Process command in PowerShell lists all the computer processes.

For more detailed process information, you will have to specify other parameters, such as Process ID (PID) or the name of the process.

Specifying process name in Get-Process command in PowerShell.

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.

Get-service command in PowerShell window with a full list of services.

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 with initials typed in PowerShell window.

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
Get AppxPackage command to see the whole list of packages in PowerShell.

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
Installing Minesweeper game from PowerShell in Windows.

Wait a few seconds or minutes for the package to install through your PowerShell. It will be available later in Windows.

Minesweeper package installation in process.

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."
Creating new directory using mkdir command in PowerShell and pointing to that new directory using cd.

The newly created folder is always located along the path C:\Windows\System32, but you could use cd to point to a different folder.

New folder created using PowerShell command visible in File Explorer System32.

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."
md and rmdir commands used in PowerShell window to create and remove empty folders.

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.

  1. 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"
  1. 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."
New-item command in use in PowerShell to create new folder and file with text.
  1. The newly created file can be found in its destination folder.
Essential Powershell Commands New Item In File Explorer

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)"
Remove item from a folder in PowerShell.

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'))
Installing Chocolatey in PowerShell using Set-ExecutionPolicy command.

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"
Select and sort objects based on the predefined Get-process in PowerShell.

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"
Stopping WordPad process from PowerShell in Windows.

Likewise, to start a new application directly from the PowerShell window, enter the Start-process cmdlet and -FilePath flag.

Start-Process -FilePath "Application name"
Start process in PowerShell to open WordPad application.

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"
Essential Powershell Commands Suspend Service

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
Suspend services which can be suspended in PowerShell window.

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"
Essential Powershell Commands Testpath

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)
Essential Powershell Commands Wait Process

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 console
  • Get-Host gives full details of the program hosting PowerShell in Windows
  • Test-JSON is an advanced cmdlet used to test whether a string is a valid JSO object
  • Trace-command traces the entire expression or command to find the parameters and flags it contains
  • Write-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.

Sayak Boral
Sayak Boral

Sayak Boral is a technology writer with over eleven years of experience working in different industries including semiconductors, IoT, enterprise IT, telecommunications OSS/BSS, and network security. He has been writing for MakeTechEasier on a wide range of technical topics including Windows, Android, Internet, Hardware Guides, Browsers, Software Tools, and Product Reviews.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى