×

Search anything:

Comprehensive list of commands for Windows PowerShell

Internship at OpenGenus

Get this book -> Problems on Array: For Interviews and Competitive Programming

PowerShell is a powerful command-line tool that is included with Microsoft Windows. It provides a flexible and efficient way to manage and automate tasks on your system, from basic file operations to complex administration tasks. PowerShell uses a command-line interface to interact with the system and execute commands.

For many users, however, getting started with PowerShell can be intimidating, given the vast range of commands and functionalities it offers. That's why we've put together a comprehensive cheat sheet containing all the most useful PowerShell commands and their descriptions.

Our PowerShell cheat sheet covers everything from basic operations like navigating directories and managing files, to more advanced tasks like managing processes and services, creating and managing users and groups, and configuring system settings.

To give you an idea of what working with PowerShell looks like, here's an example of how to list the contents of a directory using the "Get-ChildItem" cmdlet:

  Get-ChildItem C:\Users\Username\Documents

This command lists all the files and folders in the specified directory.

Whether you're a system administrator, developer, or just looking to get more out of your Windows machine, mastering PowerShell commands is an essential part of using the platform effectively. So, with this article at OpenGenus, let's dive into the world of PowerShell and explore all the powerful tools it has to offer!

NOTE: All commands would not work on normal privilidges, It is advised to shift to Admin Mode before testing these commands.

How to Access Powershell on Windows?

start

  1. To open powershell right click on the start button as shown above, A whole list of options opens up as seen below.

start-options
2. From this list select the Windows PowerShell (Admin) option to open the powershell.

powershell
3. Now a window like the one above can be seen. This is your PowerShell Command Line Interface or PowerShell CLI in short.


cmdlets:

This command allows you to get support with PowerShell

Get-Help

This command offers you a list of available PSDrives, such as c, env, hklm, hkcu, alias, etc.

Get-PSdrive

In any registry, children are the subkeys of the current key. To get the required details, you can use the following command.

Get-ChildItem

Run this command to list all the children recursively of the current PSdrive, folder, or registry key.

Get-ChildItem -recurse

Use this command To include the hidden folders (directories).

Get-ChildItem -rec -force

Run any of these commands to get the list file and directory names in the current folder.

(Get-ChildItem).name or Get-ChildItem -name

Use this command to get the number of entries in the collection of objects returned by the Get-Children.

(Get-ChildItem).count


PSdrives:

The prompt character will change to the “ENV:>”. Set-Location env by running the following command:Set-Location env-

Switching to env-

This command will get you all the environment variables.

Env:\> Get-Childitem

Use this command to get the environment variables of “userprofile.”

Env:\> Get-Childitem userprofile

Run the following command to change the prompt character to “Alias.”

Env:\> Set-Location alias:

Run this command to get all the children of all aliases.

Alias:\> Get-Childitem

Use this command to get the “C:/>” prompt again, back to the default drive.

Alias:\> Set-Location C:\

Run this command to find what alias “ls” stands for.

C:\Users\user_name>$alias:ls


Pipelines:

The pipeline consists of the following three stages.

Get-ChildItem *.txt | Where-Object length -lt 1000 | Sort-Object length

Easily sets the value of the ‘lastwritetime.year’ property to the present date and time without affecting the file’s content.

(Get-Item /Users/praashibansal/Desktop).lastwritetime.year

Provides an empty result

(Get-ChildItem data.txt.rtf -name).name ### -> null

Changes the old file names and file extensions to the new ones

"data.txt.rtf" | Rename-Item -NewName "data_new.txt.rtf"

A trivial renaming command that invokes an automatic variable

Get-ChildItem data.txt | Rename-Item -new {$_.name}

If the piped object $_ doesn't have the member property (name), you will receive an error, as parameter $_.name is null

Get-ChildItem data.txt.rtf -name | Rename-Item -new {$_.name}

Displays the list of the names of all the files that are present in the current folder sorted in alphabetical order.

Get-ChildItem | Select-Object basename | Sort-Object *

Moves all the files to the folder subdirectory

Move-Item *.txt subdirectory

Gives the error message that Move-Item lacks input

Get-ChildItem *.txt | Move-Item ..\


Alias: Working as a user on Powershell

Appends value to a file

Add-Content

Finds file content in an array

Get-Content

Changes folder, key, or PS drive

Set-Location

Clears console

Clear-Host

Delete files

Remove-Item

Lists Folder, Key, or PSDrive Children

Get-ChildItem -Path .\

Sends the array to the console, pipeline, or redirect it to the file

Write-Output

Traverses each object in a pipeline

Foreach-Object

Formats the table with selected properties for each object in each column

Format-Table

Formats the process properties by name

Format-List

Provides Cmdlet Alias

Get-Alias

Provides you with commands from the current session only

Get-Command

Retrieves all the object members

Get-Member

Provides the specified item’s properties

Get-ItemProperty .\data.txt | Format-List

Gives the current value for a specified property while using the name parameter

Get-ItemPropertyValue -Path '.\data.txt' -Name LastWriteTime

Finds session variable names and sessions

Get-Variable m*

Creates a new file, directory, symbolic link, registry key, or registry entry

New-Item -Path .\ -Name "testfile1.txt" -ItemType "file" -Value "This is a text string."

Gives the entire list of all the running processes

Get-Process

Provides the current directory’s or registry key’s location

Get-Location

Renames the old item name with the new name

Rename-Item -Path “old_name” -NewName “new_name”

Removes the specified directory, files, or registry keys

Remove-Item .\testfile1.txt

Removes the specified variable

Remove-Variable

Suspends an activity for a specified period of time

Start-Sleep

Kindly Note, This is not the ultimate list and may have missed a few commands, Additionally, it may happen that with newer windows versions some commands may be degraded or enter out-of-support state.

Comprehensive list of commands for Windows PowerShell
Share this