Skip to content

Instantly share code, notes, and snippets.

@MikeFal
Last active December 15, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeFal/3ccd4e316ab4458f826d to your computer and use it in GitHub Desktop.
Save MikeFal/3ccd4e316ab4458f826d to your computer and use it in GitHub Desktop.
Code snippet to get the most recent .bak file
#raw example
$sourcepath = 'C:\DBBackups\AdventureWorks2012'
$targetpath = 'C:\DBBackups\Archive'
#You can use 'dir' instead of Get-ChildItem
Get-ChildItem $sourcepath *.bak | Sort-Object LastWriteTime -Descending | Select-Object -First 1 | Copy-Item -Destination $targetpath
#function
function Copy-LastSQLBackup{
param([string]$sourcepath
,[string]$targetpath)
Get-ChildItem $sourcepath *.bak | Sort-Object LastWriteTime -Descending | Select-Object -First 1 | Copy-Item -Destination $targetpath
}
#Example
Copy-LastSQLBackup -sourcepath C:\DBBackups\AdventureWorks2012SMO -targetpath C:\DBBackups\Archive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment