25 basic PowerShell commands for Windows administrators
Getting started with PowerShell or just need a quick refresher? This tip lists the most common PowerShell commands, with details on when to use them.
Even though Windows PowerShell has been around for a while, there are plenty of administrators who might not venture...
Continue Reading This Article
Enjoy this article as well as all of our content, including E-Guides, news, tips and more.
willingly into the command line familiar with what a PowerShell cmdlet is. But, as Microsoft expands the functionality of PowerShell, administrators should take an interest in understanding the fundamentals of its capabilities.
Let's look at 25 basic PowerShell commands you can execute tasks with. Not only are the tasks themselves common, but the structures of the commands show off the syntax and structure of other PowerShell commands. These basic PowerShell commands should get you started on the path to becoming a master.
Common entries (just to get started)
1. cd hkcu:
Navigate the Windows Registry like the file system.
2. dir –r | select string "searchforthis"
Search recursively for a certain string within files.
3. ps | sort –p ws | select –last 5
Find the five processes using the most memory.
4. Restart-Service DHCP
Cycle a service (stop, and then restart it) like Dynamic Host Configuration Protocol (DHCP).
5. Get-ChildItem – Force
List all items within a folder.
6. Get-ChildItem –Force c:\directory –Recurse
Recurse over a series of directories or folders.
7. Remove-Item C:\tobedeleted –Recurse
Remove all files within a directory without being prompted for each.
8. (Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(2)
Restart the current computer.
Collecting information
9. Get-WmiObject -Class Win32_ComputerSystem
Get information about the make and model of a computer.
10. Get-WmiObject -Class Win32_BIOS -ComputerName .
Get information about the BIOS of the current computer.
11. Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName .
List installed hotfixes -- quick fix engineering updates (QFEs), or Windows Update files.
12. Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName .
Get the username of the person currently logged on to a computer.
13. Get-WmiObject -Class Win32_Product -ComputerName . | Format-Wide -Column 1
Find just the names of installed applications on the current computer.
14. Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress
Get IP addresses assigned to the current computer.
15. Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
Get a more detailed IP configuration report for the current machine.
16. Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=true" -ComputerName .
Find network cards with DHCP enabled on the current computer.
17. Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.EnableDHCP()}
Enable DHCP on all network adapters on the current computer.
Software management
18. (Get-WMIObject -ComputerName TARGETMACHINE -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install(\\MACHINEWHEREMSIRESIDES\path\package.msi)
Install an MSI package on a remote computer.
19. (Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='name_of_app_to_be_upgraded'").Upgrade(\\MACHINEWHEREMSIRESIDES\path\upgrade_package.msi)
Upgrade an installed application with an MSI-based application upgrade package.
20. (Get-WmiObject -Class Win32_Product -Filter "Name='product_to_remove'" -ComputerName . ).Uninstall()
Remove an MSI package from the current computer.
Machine management
21. Start-Sleep 60; Restart-Computer –Force –ComputerName TARGETMACHINE
Remotely shut down another machine after one minute.
22. (New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\printerserver\hplaser3")
Add a printer.
23. (New-Object -ComObject WScript.Network).RemovePrinterConnection("\\printerserver\hplaser3 ")
Remove a printer.
24. enter-pssession TARGETMACHINE
Enter into a remote PowerShell session -- you must have remote management enabled.
25. invoke-command -computername machine1, machine2 -filepath c:\Script\script.ps1
Use the PowerShell invoke command to run a script on a remote server.
Bonus command
26. Stop-Process -processname calc*
To dismiss a process, you can use the process ID or the process name. The -processname switch enables the use of wildcards. This is how to stop the calculator.
This article is part of
Comprehensive PowerShell guide for new and seasoned admins
Windows scripting school
Need to brush up on your PowerShell scripting know-how? It's time to go to school. Check out the archives from Christa Anderson's scripting column.