Powershell Sink

PowerShell #note/sink

Execution policy

get-executionpolicy

Get current policy

get-executionpolicy -list

Get all policies

powershell.exe -ExecutionPolicy unrestricted

Starts a new session with the session scope override

Email from powershell

$EmailTo = "_to@email.com_"
$EmailFrom = "_from@email.com_"
$Subject = "_Subject_"
$Body = "_Body text message_"
$SMTPServer = "localhost"
# $filenameAndPath = "C:\CDF.pdf"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
# $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
# $SMTPMessage.Attachments.Add($attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25)
$SMTPClient.EnableSsl = $false
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("_my@email.com_", "_mypassword_");
$SMTPClient.Send($SMTPMessage)
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Operation Completed",0,"Done",0x1)

Get directories only

Get-ChildItem C:\Foo –recurse -directory

Empty folders

Get-Childitem G:\Music –Recurse –Directory | where { $_.getdirectories().count –eq 0 –and $_.getfiles().count –eq 0 )

Now to remove them safely first:

Get-Childitem G:\Music –Recurse –Directory | where { $_.getdirectories().count –eq 0 –and $_.getfiles().count –eq 0 ) |  
Remove-Item –path $_.Fullname –whatif

Satisfied that there was nothing left but empty folders, pull off the safety switch:

Get-Childitem G:\Music –Recurse –Directory | where { $_.getdirectories().count –eq 0 –and $_.getfiles().count –eq 0 ) |  
Remove-Item –path $_.Fullname

Search for duplicates

ls "path_to_folder" -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group } | Out-File -FilePath "C:\temp\search.txt"

Delete Duplicates

ls "path_to_folder" -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group | select -skip 1 } | del

PowerShell prompt customization and themes

Oh-my-posh

In comparison with batch


# Get script directory
echo %~dp0 # In batch
echo $PSScriptRoot

# Get exit code
ping 127.0.0.1
echo %errorlevel% # In batch
echo $lastexitcode

# If the previous command is a cmdlet, you can use `$?` to know if the command succeeds. `$?` returns a boolean value.
PS> Get-ChildItem
PS> echo $?
True