Skip to content

Instantly share code, notes, and snippets.

@bigbadmoshe
Last active June 29, 2023 14:24
Show Gist options
  • Save bigbadmoshe/6074bedbe2f6993e6442e54af82da01f to your computer and use it in GitHub Desktop.
Save bigbadmoshe/6074bedbe2f6993e6442e54af82da01f to your computer and use it in GitHub Desktop.
powershell cheatsheet
# powershell cheatsheet
##############################################
# restart computer
Restart-Computer -Force # force restart
# handbrake batch processing
$filelist = Get-ChildItem D:\Video\ -Filter *.wmv
ForEach ($file in $filelist)
{
$oldfile = $file.DirectoryName + "\" + $file;
$newfile = $file.DirectoryName + "\" + $file + ".mp4";
Start-Process "C:\Program Files (x86)\myapp.exe" -ArgumentList "-i `"$oldfile`" -o `"$newfile`" " -Wait -NoNewWindow
}
# delete files older than N days
$Folder = "G:\Downloads"
Get-ChildItem $Folder -Recurse -Force -ea 0 |
Where-Object { !$_.PsIsContainer -and $_.LastWriteTime -lt (Get-Date).AddDays(-180) } |
ForEach-Object {
$_ | Remove-Item -Force
$_.FullName | Out-File C:\Users\<LOGPATH>\deletedlog.txt -Append
}
# Delete empty folders and subfolders
Get-ChildItem $Folder -Recurse -Force -ea 0 |
Where-Object { $_.PsIsContainer -eq $True } |
Where-Object { $_.getfiles().count -eq 0 } |
ForEach-Object {
$_ | Remove-Item -Force
$_.FullName | Out-File C:\log\deletedlog.txt -Append
}
# test firewall port (sftp in this case)
Test-NetConnection -ComputerName sftp.givecampus.com -Port 22
# function with args
function RunAgainstDB([String] $db, [String] $base)
{
# code here
}
# wrap code into function.
function MyFunction
{
# your code here
}
# not equal
"item 1" -ne "item 2"
# list .net versions installed on windows 10 systems
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name version -EA 0 | Where-Object { $_.PSChildName -Match '^(?!S)\p{L}' } | Select-Object PSChildName, version
# try catch blocks
try
{
# logic
}
catch
{
# generic catch all error
Write-Host "An error occurred:"
Write-Host $_.ScriptStackTrace
}
# multiline command
use backticks `
# read lines of file and run command against them
```
foreach ($line in Get-Content .\file.txt)
{
# Work here
}
```
# concat strings
Write-Host "processing scripts on $server $db"
#turn on error handling for running PS in sql server agent
$ErrorActionPreference = "Stop"
# foreach loop
```
$letterArray = "a", "b", "c", "d"
foreach ($letter in $letterArray)
{
Write-Host $letter
}
```
# export csv files from sql server
# requires module
# Install-Module -Name SqlServer
```
$server = 'SQLSERVER'
$destDir = 'C:\Dest'
$db = 'Database'
$varDate = Get-Date -Format yyyyMMdd
$outputFile = 'file_' + $varDate + '.txt'
$output = Join-Path $destDir $outputFile
Invoke-Sqlcmd -InputFile 'test.sql' -serverinstance $server -database $db `
| Export-Csv -Path $output -NoType -Delimiter '|'
```
# call powershell and pass it arguments to execute command
powershell -c "Start-ScheduledTask -TaskName 'post_image_twitterbot'"
# start task
Start-ScheduledTask -TaskName "ScanSoftware"
# Scheduled Tasks
Get-ScheduledTask # list
# path
Get-ScheduledTask -TaskPath "\UpdateTasks\*"
# single task
Get-ScheduledTask -TaskName "DailyMacPull"
# get task info
Get-ScheduledTaskInfo -TaskName "\Sample\SchedTask01"
# start scheduled task
Start-ScheduledTask -TaskName "ScanSoftware"
# start all tasks in folder
Get-ScheduledTask -TaskPath "\UpdateTasks\UpdateVirus\" | Start-ScheduledTask
# output directory
tree
#pass path
tree c:\path
# with files
tree /f
# Get history
Get-History
# move item
Move-Item -Path C:\test.txt -Destination E:\Temp\tst.txt
# query file and match pattern / Where context gives you pre,post N lines.
PS> Select-String -Pattern "case" -Context 1, 2 poet.txt
# write to output
Write-Host "******** Hello World **********"
# read input to variable
$directoryToClean = Read-Host 'Please enter the directory to clean...'
# remove spaces from files in directory (can specify files only with switch -File)
Get-ChildItem -File $directoryToClean | Rename-Item -NewName { $_.Name -replace ' ', '' }
# read file into body of email where $fullFilePath is a text file.
$emailBody = Get-Content -Path $fullFilePath -Raw
# remove duplicate lines from text file (first sort)
$filename | Sort-Object | Get-Unique > $newfileName
# get version
Get-Host | Select-Object Version
# pipe output to html
Get-Alias | ConvertTo-Html | Out-File aliases.htm
# tail file
Get-Content ./log.log -Tail 10
# gets the last 10 lines of the file and waits for more
Get-Content ./log.log -Wait -Tail 10
# Also, for those *nix users, note that most systems alias cat to Get-Content, so this usually works
Get-Content \\<DIRECTORY>\<FILE.log> -Tail 25
# tail and grep using regex eg grab only date
Get-Content .\info.log -Tail 25 | Where-Object { $_ -match "2022-03-21" }
# get date and format
Get-Date
Get-Date -Format yyyyMMdd
# output: 20190405
# assign to variable
$varDate = Get-Date -Format s
Write-Output $varDate
# output: 2019-04-05T08:01:29
# convert object to string
$object_variable | Out-String
# Send an email (with no body) from User01 to User02: (missing smtp server)
Send-MailMessage -To "User01 <[email protected]>" -From "User02 <[email protected]>" -Subject "Test mail" -Body "hello world" -SmtpServer "<SERVER.DOMAIN.COM>"
# add for html, preface -body with:
-bodyashtml
# assign variable with dollar sign $
$varTemp = "whatever"
# get last write time of file list and pipe to foreach
Get-ChildItem $directory | ForEach-Object { $_.LastAccessTime }
# get files last written to within the last 7 days
Get-ChildItem $directory | Where-Object { $_.LastWriteTime -gt (Get-Date).AddDays(-7) }
# check if variable is null (returns True / False)
$variable -eq $null
# check if is greater than
if (1 -gt 0)
{}
# call exe from powershell
Start-Process -FilePath "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"
# and add redirect output
-RedirectStandardOutput "Testsort.txt"
# Syntax for IF STATEMENT
if ( condition ) { commands_to_execute }
elseif ( condition2 ) { commands_to_execute }
else { commands_to_execute }
# Key
# Condition An expression that will evaluate to true or false,
# often utilising one or more comparison operators.
# commands_to_execute
# A PowerShell or external command to run if the condition is true.
# run tfs cli and email output
C:
Set-Location "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE"
$lastfifty = .\tf history $/<TFS PROJECT> /format:brief /noprompt /recursive /stopafter:50 | Out-String
$dateSent = Get-Date -Format yyyy-MM-dd
$vSubject = "TFS Log " + $dateSent
$vBody = $lastfifty
Send-MailMessage -To "YOURNAME <[email protected]>", "YOURNAMETWO <[email protected]>" -Cc "YOUR NAME <[email protected]>" -From "FROM EMAIL <[email protected]>"-Subject $vSubject -Body $vBody -SmtpServer "mail.SMTPSERVER.com"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment