Common Server CMDlets
Process (Replacing your task manager)
Get all processes
get-process
If you are a linux fan
get-alias ps,kill
Stop a specifc process
get-process -name notepad | stop-process
ps notepad | kill
Browse Files (Replacing your explorer)
A lot of aliases faking windows and linux bash shell
get-alias dir,ls,move,mv,del,rm,cd,copy,cp
Get Files
get-childitem
Get Files Recursive
get-childitem -Recurse
get-childitem -Recurse -Depth 2
Get Files with filter
get-childitem -Recurse -filter "*.txt" -path c:\d
Get Complete path
get-childitem -Recurse -filter "*.txt" -path c:\d | select fullname
Making a path (recursive)
New-Item -ItemType Directory "c:\hello\word\path" -erroraction SilentlyContinue
Testing, joining, splitting a path with test-path, join-path, split-path (exists)
$directory = "c:\hello\word\path"
if( Test-Path -PathType Container -path $directory ) {
write-host "Directory exists $directory"
$file = join-path -path $directory -childpath "myfile.txt"
echo "" > $file
if ( test-path -pathtype leaf -path $file) {
write-host "File $file also exists"
split-path -Leaf $file
split-path -Parent $file
}
}
Common directories
Special folders: User Main, User Desktop, User Documents, User Appdata System32 folder, Program Files, System application data (mostly c:\programdata)
[System.Enum]::GetValues([System.Environment+SpecialFolder])
[System.Environment]::GetFolderPath("UserProfile")
[System.Environment]::GetFolderPath("Desktop")
[System.Environment]::GetFolderPath("MyDocuments")
[System.Environment]::GetFolderPath("ApplicationData")
[System.Environment]::GetFolderPath("System")
[System.Environment]::GetFolderPath("ProgramFiles")
[System.Environment]::GetFolderPath("CommonApplicationData")
Complete Overview.aspx)
Getting your volumes
get-volume
Services (Replacing services.msc)
Getting the services
get-service
Stopping services
get-service -Name WSearch | stop-service
Starting services
get-service -Name WSearch | start-service
Disabling services as admin
get-service -Name WSearch | Select starttype
get-service -Name WSearch | Set-Service -StartupType disabled
get-service -Name WSearch | Select starttype
get-service -Name WSearch | Set-Service -StartupType automatic
get-service -Name WSearch | Select starttype
Registry (Replacing regedit)
Powershell represent a lot of objects like drives (registry inclusive)
get-drive
However, use get-childitem to query subdirectory
Get-ChildItem HKCU:\Software
But to query properties, use get-item
Get-Item HKCU:\Software\RegisteredApplications
Get-Item HKCU:\Software\RegisteredApplications | Get-ItemProperty
Get-Item HKCU:\Software\RegisteredApplications | Get-ItemProperty -name Firefox
Directly get the properties
Get-ItemProperty -Path HKCU:\Software\RegisteredApplications -Name firefox
Get-ItemPropertyValue -Path HKCU:\Software\RegisteredApplications -Name firefox
Creating items (run as admin)
$coursesettings = new-item -ItemType Directory HKLM:\SOFTWARE\PowerStartCourse\
$newsetting = $coursesettings | New-ItemProperty -Type String -Name "level" -Value "Expert"
$coursesettings | get-item
Stuff running at boot
$runatboot = Get-Item -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
$paths = $runatboot.GetValueNames() | % { $n = $_;new-object -type psobject -property @{Name=$_;ExecPath=(get-itempropertyvalue -path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run -name $n)}}
See if the Veeam Agent for Windows is/was installed
$agentpath = (Get-ItemPropertyValue -path 'HKLM:\SOFTWARE\Veeam\Veeam Endpoint Backup\' -Name installdir -ErrorAction SilentlyContinue)
if ($agentpath -ne $null -and (test-path -type container $agentpath)) {
write-host "Agent is or was installed under $agentpath"
$configurator = get-childitem -path $agentpath -filter "Veeam.Agent.Configurator.exe"
if ($configurator -ne $null) {
write-host "Did find configurator $configurator"
}
}
Environment variables
list all environment variables with get-childitem
get-childitem env:
get a specific value with the $env prefix
$env:path
for example splitting the path
$env:path -split ";"
or getting the powershell module path
$env:PSModulePath -split ";"
some wizard stuff to know your profile module path
$env:PSModulePath -split ";" | ? { $_ -match ([System.Environment]::GetFolderPath("UserProfile") -replace "\\","\\") }
you can also use dotnet, which gives you more control
$envvariables = [System.Environment]::GetEnvironmentVariables()
foreach ($key in $envvariables.Keys) {
$v = $envvariables[$key]
write-host ("{0,-30} {1}" -f $key,$v)
}
especially interesting if you want to set
[System.Environment]::SetEnvironmentVariable
[System.Enum]::GetValues([System.EnvironmentVariableTarget])