SharePoint Powershell to backup web.config with date time stamp

param( [string] $fileName)
$location=$filename-replace “web.config$”,””
if (-not(Test-Path $fileName)) {break}
“Original filename: $fileName”
$fileObj = get-item $fileName
$DateStamp = get-date -uformat “%Y-%m-%d@%H-%M-%S”
$extOnly = $fileObj.extension
if ($extOnly.length -eq 0) {
$nameOnly = $fileObj.Name

copy “$fileObj” “$fileObj$con”
rename-item “$fileObj$con” “$location$nameOnly-$DateStamp”
else {
$nameOnly = $fileObj.Name.Replace( $fileObj.Extension,”)
copy “$fileObj” “$fileObj$con”
rename-item “$fileObj$con” “$location$nameOnly-$DateStamp$extOnly”
“New filename: $nameOnly-$DateStamp$extOnly”

Save this powershell script as BackupWebConfig.ps1

Then run the script like

./backupwebconfig.ps1 “C:\inetpub\wwwroot\wss\VirtualDirectories\foldername\web.config”

Replace the folder with your config file location and run it

Leave a Reply

Your email address will not be published. Required fields are marked *