Most Useful PowerShell Scripts

40 Most Common PowerShell Commands

Introduction to PowerShell

PowerShell is a powerful command-line shell and scripting language designed for system administration and automation. It’s an essential tool for IT professionals. In this guide, we will cover the 40 most commonly used PowerShell commands, complete with descriptions and code snippets.

PowerShell Command List

Below are the 40 essential PowerShell commands you should know:

1. Get-Help

    
Get-Help

The Get-Help cmdlet provides help information for cmdlets, functions, workflows, and scripts. It’s a quick way to understand how a command works.

2. Get-Process

    
Get-Process

Displays a list of all running processes on your machine. This is helpful for monitoring system performance and resource usage.

3. Set-ExecutionPolicy

    
Set-ExecutionPolicy RemoteSigned

Changes the user’s script execution policy to control the ability to run scripts on the system. This is crucial for security.

4. Get-Service

    
Get-Service

This command allows you to retrieve the status of all services running on your system.

5. Start-Service

    
Start-Service -Name "ServiceName"

Used to start a service on your system. Replace "ServiceName" with the actual service name.

6. Stop-Service

    
Stop-Service -Name "ServiceName"

This command is used to stop a service on your system.

7. Set-ItemProperty

    
Set-ItemProperty -Path "C:\Path\To\File" -Name "PropertyName" -Value "NewValue"

Changes properties of items like files, directories, or registry keys

8. Get-EventLog

    
Get-EventLog -LogName "Application"

Retrieve entries from event logs. This is important for troubleshooting and monitoring system events.

9. Get-Command

    
Get-Command

Displays all cmdlets, functions, workflows, aliases, and scripts available in the session.

10. Get-Content

    
Get-Content "C:\Path\To\File.txt"

Reads the content of a file, allowing you to display it on the screen.

11. Set-Content

    
Set-Content "C:\Path\To\File.txt" -Value "New Content"

Writes new content to a file.

12. Get-ADUser

    
Get-ADUser -Identity "username"

Writes new content to a file.

13. New-Item

    
New-Item -Path "C:\Path" -Name "NewFolder" -ItemType Directory

Creates a new item, such as a file or folder.

14. Remove-Item

    
Remove-Item "C:\Path\To\File.txt"

Deletes files or folders from the system

15. Get-Process -Name

    
Get-Process -Name "processname"

Returns information about a specific process running on the system.

16. Get-FileHash

    
Get-FileHash "C:\Path\To\File.txt"

Retrieves an item, such as a file or directory, from the specified path.

17. Get-Item

    
Get-Item "C:\Path\To\File.txt"

Retrieves an item, such as a file or directory, from the specified path.

18. Set-Item

    
Set-Item "C:\Path\To\File.txt" -Value "New Value"

Sets the value of an item like a file or registry key.

19. Test-Connection

    
Test-Connection "hostname"

Tests network connectivity to a remote host.

20. Get-PSDrive

    
Test-Connection "hostname"

Displays all the drives available in the current session, including file system, registry, and environment drives.

 

21. Clear-Host

    
Clear-Host

Clears the console screen.

22. Get-Date

    
Get-Date

Displays the current date and time

23. Get-Help

    
Get-Help "cmdlet"

Provides detailed help for a specific cmdlet.

24. Export-Csv

    
Export-Csv -Path "C:\Path\To\File.csv" -NoTypeInformation

Exports data to a CSV file.

25. Import-Csv

    
Import-Csv -Path "C:\Path\To\File.csv"

Imports data from a CSV file.

26. Get-EventLog

    
Get-EventLog -LogName "System" -Newest 10

Shows the most recent events from a specific log.

27. Set-Alias

    
Set-Alias "aliasName" "cmdletName"

Creates an alias for a cmdlet, simplifying repeated commands.

28. Get-Help Command

    
Get-Help Get-Process

Displays help for a specific cmdlet or function.

.

29. New-PSDrive

    
New-PSDrive -Name "DriveName" -PSProvider FileSystem -Root "C:\"

Creates a new PowerShell drive.

.

30. Remove-PSDrive

    
Remove-PSDrive -Name "DriveName"

Removes a PowerShell drive.

 

.

31. Out-File

    
Out-File "C:\Path\To\Output.txt"

Redirects output to a file.

32. Select-Object

    
Select-Object -Property "Property1", "Property2"

Selects specific properties from an object or collection.

33. Invoke-Command

    
Invoke-Command -ScriptBlock { Get-Process }

Executes a command on a remote system.

34. Get-Location

    
Get-Location

Displays the current directory.

 

35. Set-Location

    
Set-Location "C:\NewDirectory"

Changes the current directory.

36. Get-Help Get-Command

    
Get-Help Get-Command

Displays detailed help for the Get-Command cmdlet.

37. Add-Content

    
Add-Content "C:\Path\To\File.txt" -Value "Additional Content"

Appends content to an existing file.

 

38. Test-Path

    
Test-Path "C:\Path\To\File.txt"

Checks if a file or directory exists.

 

39. ForEach-Object

    
ForEach-Object { $_ }

Performs an operation on each item in a collection.

40. Where-Object

    
Where-Object { $_.Property -eq "Value" }

Filters objects based on specified conditions.

Conclusion

PowerShell is an invaluable tool for automation and system management. These 40 commands cover the basics, but PowerShell’s capabilities extend far beyond this list. Keep experimenting and exploring to unlock its full potential.

Related Posts