Last active
December 15, 2015 21:49
-
-
Save MikeFal/3ccd4e316ab4458f826d to your computer and use it in GitHub Desktop.
Code snippet to get the most recent .bak file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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