Compress a Directory with 7-zip and Powershell

Ensure you have 7-zip installed, obviously.  I adapted this from some other code I found on the internets, but unfortunately I can’t find the original post, so apologies for not giving the original author props.

# Alias for 7-zip
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"}
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"

$path = "C:\Test\ScriptDB\"

foreach($dir in gci -Path $path | where{$_.PSIsContainer})
	$zipFile = $path + $dir.Name + ".zip"
	sz a -tzip "$zipfile" $dir.FullName
	Remove-Item $dir.FullName -Recurse -Force

ConsoleColor error in Powershell job step in Sql Server

If you see an error like this when you execute a powershell job in sql server, it is due to an errant cls in your powershell script.  Remove the cls and it should execute correctly.  There is no screen to clear, hence it errors out.

A job step received an error at line 3 in a PowerShell script. The corresponding line is ‘$space.ForegroundColor = $host.ui.rawui.ForegroundColor’. Correct the script and reschedule the job. The error information returned by PowerShell is: ‘Exception setting "ForegroundColor": "Cannot convert null to type "System.ConsoleColor" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White"."  ‘.