Essential Windows Powershell commands for beginners

General questions and comments relating to Powershell.
Post Reply
bravertimp
Posts: 3
Joined: Fri Jul 05, 2019 4:10 pm

Tue Jul 09, 2019 4:59 pm

Essential Windows Powershell commands for beginners



Navigate the Windows Registry like the file system:

Code: Select all

cd hkcu:
Search recursively for a certain string within files:

Code: Select all

dir –r | select string "searchforthis"
Find the five processes using the most memory:

Code: Select all

ps | sort –p ws | select –last 5
Cycle a service (stop, and then restart it) like DHCP:

Code: Select all

Restart-Service DHCP
List all items within a folder:

Code: Select all

Get-ChildItem – Force
Recurse over a series of directories or folders:

Code: Select all

Get-ChildItem –Force c:\directory –Recurse
Remove all files within a directory without being prompted for each:

Code: Select all

Remove-Item C:\tobedeleted –Recurse
Restart the current computer:

Code: Select all

(Get-WmiObject -Class Win32_OperatingSystem -ComputerName .).Win32Shutdown(2)
Get information about the make and model of a computer:

Code: Select all

Get-WmiObject -Class Win32_ComputerSystem
Get information about the BIOS of the current computer:

Code: Select all

Get-WmiObject -Class Win32_BIOS -ComputerName .
List installed hotfixes -- QFEs, or Windows Update files:

Code: Select all

Get-WmiObject -Class Win32_QuickFixEngineering -ComputerName .
Get the username of the person currently logged on to a computer:

Code: Select all

Get-WmiObject -Class Win32_ComputerSystem -Property UserName -ComputerName .
Find just the names of installed applications on the current computer:

Code: Select all

Get-WmiObject -Class Win32_Product -ComputerName . | Format-Wide -Column 1
Get IP addresses assigned to the current computer:

Code: Select all

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Format-Table -Property IPAddress
Get a more detailed IP configuration report for the current machine:

Code: Select all

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
Find network cards with DHCP enabled on the current computer:

Code: Select all

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DHCPEnabled=true" -ComputerName .
Enable DHCP on all network adapters on the current computer:

Code: Select all

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName . | ForEach-Object -Process {$_.EnableDHCP()}
Install an MSI package on a remote computer:

Code: Select all

(Get-WMIObject -ComputerName TARGETMACHINE -List | Where-Object -FilterScript {$_.Name -eq "Win32_Product"}).Install(\\MACHINEWHEREMSIRESIDES\path\package.msi)
Upgrade an installed application with an MSI-based application upgrade package:

Code: Select all

(Get-WmiObject -Class Win32_Product -ComputerName . -Filter "Name='name_of_app_to_be_upgraded'").Upgrade(\\MACHINEWHEREMSIRESIDES\path\upgrade_package.msi)
Remove an MSI package from the current computer:

Code: Select all

(Get-WmiObject -Class Win32_Product -Filter "Name='product_to_remove'" -ComputerName . ).Uninstall()
Remotely shut down another machine after one minute:

Code: Select all

Start-Sleep 60; Restart-Computer –Force –ComputerName TARGETMACHINE
Add a printer:

Code: Select all

(New-Object -ComObject WScript.Network).AddWindowsPrinterConnection("\\printerserver\hplaser3")
Remove a printer:

Code: Select all

(New-Object -ComObject WScript.Network).RemovePrinterConnection("\\printerserver\hplaser3 ")
Enter into a remote PowerShell session -- you must have remote management enabled:

Code: Select all

enter-pssession TARGETMACHINE
Use the PowerShell invoke command to run a script on a remote servers:

Code: Select all

invoke-command -computername machine1, machine2 -filepath c:\Script\script.ps1
Post Reply

Social Media