PowerShell Cheat Sheet

PowerShell is a command-line shell and scripting language built on .NET that is designed to automate system tasks and manage configurations. This cheat sheet provides a comprehensive list of common PowerShell commands and examples to help you streamline your workflows and automate tasks.

Basic PowerShell Commands

1. Get Help

To get help on any command or topic:

Get-Help

To get detailed help with examples:

Get-Help -Detailed

2. Listing Commands

To list all available PowerShell commands:

Get-Command

To find a command by name or partial name:

Get-Command *Service*

3. Get System Information

To retrieve basic system information:

Get-ComputerInfo

To get specific information about the operating system:

Get-WmiObject -Class Win32_OperatingSystem

Working with Files and Directories

4. Get Current Directory

To show the current directory:

Get-Location

5. Change Directory

To change to a different directory:

Set-Location "C:\path\to\directory"

6. List Files and Folders

To list all files and folders in the current directory:

Get-ChildItem

To list files recursively (all files and folders in subdirectories):

Get-ChildItem -Recurse

7. Create a New File

To create an empty file:

New-Item -Path "C:\path\to\file.txt" -ItemType File

8. Create a New Directory

To create a new directory:

New-Item -Path "C:\path\to\newfolder" -ItemType Directory

Managing Processes and Services

9. List Running Processes

To get a list of currently running processes:

Get-Process

10. Stop a Process

To stop a specific process by name:

Stop-Process -Name "notepad"

To stop a process by its process ID:

Stop-Process -Id 1234

11. Start a Process

To start a process by name:

Start-Process "notepad.exe"

12. Get Service Status

To list the status of all services:

Get-Service

To get the status of a specific service:

Get-Service -Name "Spooler"

13. Start and Stop Services

To start a service:

Start-Service -Name "Spooler"

To stop a service:

Stop-Service -Name "Spooler"

Managing Users and Groups

14. Get User Information

To get information about the current user:

Get-Whoami

15. Create a New User

To create a new user account:

New-LocalUser -Name "username" -Password (ConvertTo-SecureString "password" -AsPlainText -Force)

16. Add User to Group

To add a user to a local group:

Add-LocalGroupMember -Group "Administrators" -Member "username"

Working with PowerShell Scripts

17. Running a Script

To run a PowerShell script:

.\script.ps1

If execution policy blocks script execution, you may need to change it:

Set-ExecutionPolicy RemoteSigned

18. Execution Policy Levels

The four main execution policy levels are:

  • Restricted: No scripts can run.
  • AllSigned: Only scripts signed by a trusted publisher can be run.
  • RemoteSigned: Scripts downloaded from the internet must be signed by a trusted publisher.
  • Unrestricted: All scripts can run, but a warning appears for scripts downloaded from the internet.

Networking Commands

19. Get IP Configuration

To view the system's network configuration:

Get-NetIPAddress

20. Test Network Connection

To test network connectivity (similar to ping):

Test-Connection -ComputerName www.google.com

File Content and Data Management

21. Reading File Contents

To display the contents of a file:

Get-Content "C:\path\to\file.txt"

22. Write to a File

To write or append text to a file:

Add-Content "C:\path\to\file.txt" "This is a new line"

23. Search Inside a File

To search for text within a file:

Select-String -Path "C:\path\to\file.txt" -Pattern "searchText"

Active Directory Management

24. Get Active Directory User Information

To get information about a specific Active Directory user:

Get-ADUser -Identity "username"

25. Create a New AD User

To create a new Active Directory user:

New-ADUser -Name "John Doe" -SamAccountName "jdoe" -UserPrincipalName "jdoe@domain.com"

26. Add a User to an AD Group

To add a user to an Active Directory group:

Add-ADGroupMember -Identity "GroupName" -Members "username"

Security and Permissions

27. Set Permissions for a File or Folder

To set file or folder permissions:

icacls "C:\path\to\folder" /grant "UserName:(OI)(CI)F"

28. Check User Permissions

To check the permissions for a specific user on a file or folder:

icacls "C:\path\to\folder" /findsid "username"