PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language.
Can be run as Jobs in SQL Server.
Example
Run Script From SQL Job
Type: Operating system (CmdExec)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "C:\MyDirectory01\MyScript.ps1"Remove all files greater than a certain date.
Type: PowerShell
$Path = "C:\MSSQL\DBBackups\"
$Daysback = "-7"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($Daysback)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete -and $_.Extension -eq ".bak" } | Remove-Item -Recurse -ErrorAction  SilentlyContinuePowerShell Commands
Here are 25 basic PowerShell commands:
| Command name | Alias | Description | 
|---|---|---|
| Set-Location | cd, chdir, sl | Sets the current working location to a specified location. | 
| Get-Content | cat, gc, type | Gets the content of the item at the specified location. | 
| Add-Content | ac | Adds content to the specified items, such as adding words to a file. | 
| Set-Content | sc | Writes or replaces the content in an item with new content. | 
| Copy-Item | copy, cp, cpi | Copies an item from one location to another. | 
| Remove-Item | del, erase, rd, ri, rm, rmdir | Deletes the specified items. | 
| Move-Item | mi, move, mv | Moves an item from one location to another. | 
| Set-Item | si | Changes the value of an item to the value specified in the command. | 
| New-Item | ni | Creates a new item. | 
| Start-Job | sajb | Starts a Windows PowerShell background job. | 
| Compare-Object | compare, dif | Compares two sets of objects. | 
| Group-Object | group | Groups objects that contain the same value for specified properties. | 
| Invoke-WebRequest | curl, iwr, wget | Gets content from a web page on the Internet. | 
| Measure-Object | measure | Calculates the numeric properties of objects, and the characters, words, and lines in string objects, such as files … | 
| Resolve-Path | rvpa | Resolves the wildcard characters in a path, and displays the path contents. | 
| Resume-Job | rujb | Restarts a suspended job | 
| Set-Variable | set, sv | Sets the value of a variable. Creates the variable if one with the requested name does not exist. | 
| Show-Command | shcm | Creates Windows PowerShell commands in a graphical command window. | 
| Sort-Object | sort | Sorts objects by property values. | 
| Start-Service | sasv | Starts one or more stopped services. | 
| Start-Process | saps, start | Starts one or more processes on the local computer. | 
| Suspend-Job | sujb | Temporarily stops workflow jobs. | 
| Wait-Job | wjb | Suppresses the command prompt until one or all of the Windows PowerShell background jobs running in the session are … | 
| Where-Object | ?, where | Selects objects from a collection based on their property values. | 
| Write-Output | echo, write | Sends the specified objects to the next command in the pipeline. If the command is the last command in the pipeline,… | 
Comments