PowerShell Tips and tricks

Here is where I will post any valuable one liners I find.
Gather Internet favorites
001
Get-ChildItem $env:USERPROFILE\Favorites -Recurse

Set the $computername variable to contain all windows servers on the domain.

001
$Computername = ( Get-ADComputer -filter 'OperatingSystem -like "*Server*"' | Select-Object -expand Name )

Creates a .CSV with username,diplayname,email address and employee numbers

001
Get-ADUser -Filter * -Properties * | Where-Object{$_.Enabled -eq "True"| select EmployeeNumber, DisplayName, SAMAccountName, mail| sort DisplayName ConvertTo-Csv $env:USERPROFILE\desktop\AD.csv
 
Ping all Windows 7 computers on the network
001
002
003
004
005
## ping all windows 7 computers on the network
$File = "$env:USERPROFILE\Desktop\computers.txt"
Get-ADComputer -Filter 'OperatingSystem -like "*7*"' | Select-Object -ExpandProperty name | Out-File $File
$computers = Get-Content ($File)
foreach( $c in $Computers ){ ping $c -n 1 }
beep noise

001
$([char]7)

how to specify a source when installing a GUI
001
Install-WindowsFeature -Name Server-Gui-Mgmt-Infra, Server-Gui-Shell –Restart –Source wim:D:\sources\install.wim:4 -restart

Allow the magic to happen
001
Enable-PSRemoting -force ; Set-ExecutionPolicy remotesigned -force

Checks all computers on the domain to see if PS-Remoting is enabled
001
Invoke-Command -computername ( Get-ADComputer -filter * | ForEach-Object { $_.name } ) -scriptblockhostname } -ErrorAction SilentlyContinue

Opens An Administrative PowerShell Prompt
001
Start-Process PowerShell -Verb RunAs

Retrieves all AD usernames, telephone numbers and email addesses and export to csv on the desktop
001
002
003
$filepath = "$env:USERPROFILE\Desktop\AD_NAMES.csv"
$users = Get-ADUser -Filter * -Properties * | Select-Object displayname, HomePhone, MobilePhone, mail | sort DisplayName
$users | Export-Csv $filepath

Change a drive letter PSv3.0+
001
Get-Partition -DriveLetter S | Set-Partition -NewDriveLetter D

Change a drive letter PSv2.0+
001
002
$drive = Get-WmiObject -Class win32_volume -Filter {DriveLetter = "G:"}
Set-WmiInstance -input $drive -Arguments @{DriveLetter="D:"; Label="Kerfoot's 1TB"}

Modifies the PS console foreground color

001
$Host.PrivateData.VerboseForegroundColor = "Green"

Modifies the PS console background color
001
$Host.PrivateData.VerboseBackgroundColor = "Blue"

Pauses a script for 10 seconds
001
wait-event -timeout 10

Displays the version of PowerShell you are currently in
001
$PSVersionTable

transpose from vertical list to horizontal comma separated
001
002
Get-Content file.txt-join "," | out-file commas.txt

Set a service to manual start up
001
Set-Service VSS -startuptype "manual"

Set a service to automatic start up
001
Set-Service VSS -startuptype "automatic"

Changes the PS console Prompt
001
function prompt {"PS [www.matthewkerfoot.com]> "}

Send an Email via PowerShell
001
002
003
004
Send-MailMessage -From SendingAddress@mailserver.com
                 -To mkkerfoot@gmail.com
                 -Subject "Email Subject"
                 -SmtpServer servername@domain.com

Retreive the Desktop path
001
[environment]::GetFolderPath("Desktop")

Get time until date
001
new-timespan -end (get-date -year 2015 -month 12 -day 25| format-table -AutoSize

Gets an example from a random command
001
Get-Command | Get-Random | Get-Help | Out-String

Get all alias'
001
get-alias *

This resolved the FIPS error I receive when using ConnectWise.
001
002
003
cd HKLM:\
Remove-ItemProperty "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy\Enabled"
New-ItemProperty "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy" -Name "Enabled" -Value 0 -PropertyType "DWORD"

Get-Date alternative
001
[datetime]::Now

gets all GPO assigned to this computer, must be ran from a DC.
001
Get-GPO -All | Select-Object DisplayName,GpoStatus,CreationTime | Format-Table

List PhysicalDisk counter paths
001
(Get-Counter -listset physicaldisk).paths

List Memory counters
001
Get-Counter -ListSet *MSExchange* | Select-Object -ExpandProperty Counter

get all installed updates
001
Get-HotFix | Format-Table -AutoSize

Flip a name
001
"Matthew Kerfoot" -replace "([a-z]+)\s([a-z]+)",'$2, $1'

Retrieves newest 100 system event logs
001
Get-EventLog -LogName System -Newest 100

Bypasses the execution policy and runs a script
001
powershell.exe -ExecutionPolicy ByPass -File .\filename.ps1

Get-EventLog for a specific date\time.
001
002
003
$before = get-date 12/03/14
$after = get-date 12/02/13
Get-EventLog -LogName System -Before $before -After $after | Where-Object {$_.EntryType -ne "Information"| Select-Object Index, EntryType, InstanceId, Message, Source, TimeGenerated

Lists all counter paths
001
(Get-Counter -listset *).paths

Gets SQL performance counter data from local and remote computers.
001
002
003
004
005
006
Get-Counter -Counter "\SQLServer:General Statistics\User Connections"
Get-Counter -Counter "\SQLServer:Memory Manager\Maximum Workspace Memory (KB)" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\SQLServer:Databases(*)\Transactions/sec" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\SQLServer:Buffer Manager\Buffer cache hit ratio" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\SQLServer:Wait Statistics(Average wait time (ms))\Lock waits" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\SQLServer:Wait Statistics(*)\Lock waits" -SampleInterval 1 -MaxSamples 3

Gets Memory performance counter data from local and remote computers.
001
002
Get-Counter -Counter "\Memory\Pages/sec" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\Memory\Available MBytes" -SampleInterval 1 -MaxSamples 3

Gets Logical Disk performance counter data from local and remote computers.
001
002
003
Get-Counter -counter "\LogicalDisk(*)\Disk Transfers/sec" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\LogicalDisk(*)\% Disk Time" -SampleInterval 1 -MaxSamples 3
Get-Counter -Counter "\LogicalDisk(_Total)\% Free Space" -SampleInterval 1 -MaxSamples 3

Gets Physical Disk performance counter data from local and remote computers.
001
Get-Counter -Counter "\PhysicalDisk(*)\Avg. Disk Bytes/Transfer" -SampleInterval 1 -MaxSamples 3

Gets CPU performance counter data from local and remote computers.
001
Get-Counter -counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 3 ; wmic cpu get loadpercentage

Check to see if Integration services are enabled within VM (hyper-v), if enabled copy\paste to host\child vmm is allowed.
001
Get-VM * | Get-VMIntegrationService | ? {-not($_.Enabled)}

Download WMF 4.0 via PowerShell 3.0
001
((new-object net.webclient).DownloadFile("http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe","dotNetFx40_Full_x86_x64.exe")) & cd $env:USERPROFILE & .\dotNetFx40_Full_x86_x64.exe /q