So I haven’t written anything even mildly technical lately so I thought I’d show a small script I use to copy and restore my scripts and PowerShell profile across multiple machines. It’s fairly straight forward. It will backup my scripts directory from my desktop and PowerShell folder from my documents directory; and recreates the the folder structure in the directory the script is run from.
Conversely it will restore as well provided the content exists. It is USER specific, so if you use multiple user id’s like I do, it’s important that you manage them accordingly.
Option Explicit
On Error Resume Next
Dim oWShell, oFSO, oNet
Dim strCurUser, strSourceDrive, strTargetDrive, strOption, strMsg
Set oWShell = CreateObject("Wscript.Shell")
Set oFSO = CreateObject("Scripting.Filesystemobject")
Set oNet = CreateObject("Wscript.Network")
strCurUser = oNet.UserName
strTargetDrive = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName)- _
Len(WScript.ScriptName)) & strCurUser
strSourceDrive = oWShell.ExpandEnvironmentStrings("%userprofile%")
strMsg = "Filecopy Summary: "& vbcrlf
strOption = LCase(InputBox("Restore|Backup"))
Select Case strOption
Case "restore" Call Restore()
Case Else Call Backup()
End Select
WScript.Echo strMsg
'-------------------
Function Backup()
Dim strScripts, strPShell
strScripts = "\Desktop\Scripts" : strPShell = "\Documents\windowspowershell"
If Not CreateFolder(strTargetDrive & strScripts) Then
strMsg = strMsg & "Failed Creating: "& strTargetDrive & strScripts & vbCrLf
Else
strMsg = strMsg & "Created: " & strTargetDrive & strScripts & vbCrlf
End If
If Not CreateFolder(strTargetDrive & strPShell) Then
strMsg = strMsg & "Failed Creating: "& strTargetDrive & strPShell & vbCrLf
Else
strMsg = strMsg & "Created: " & strTargetDrive & strPShell & vbCrlf
End If
If Not CopyFolder(strSourceDrive & strScripts, strTargetDrive & strScripts) Then
strMsg = strMsg & "Failed to Copy: " & strSourceDrive & strScripts & vbCrLf
Else
strMsg = strMsg & "Copied: " & strSourceDrive & strScripts & vbCrLf
End If
If Not CopyFolder(strSourceDrive & strPShell, strTargetDrive & strPShell) Then
strMsg = strMsg & "Failed to Copy: " & strSourceDrive & strPShell & vbCrLf
Else
strMsg = strMsg & "Copied: " & strSourceDrive & strPShell & vbCrLf
End If
End Function
'-------------------
Function Restore()
Dim strScripts, strPShell, temp1, temp2
strScripts = "\Desktop\Scripts" : strPShell = "\Documents\windowspowershell"
temp1 = strSourceDrive : temp2 = strTargetDrive
strTargetDrive = temp1 : strSourceDrive = temp2
If Not CreateFolder(strTargetDrive & strScripts) Then
strMsg = strMsg & "Failed Creating: "& strTargetDrive & strScripts & vbCrLf
Else
strMsg = strMsg & "Created: " & strTargetDrive & strScripts & vbCrlf
End If
If Not CreateFolder(strTargetDrive & strPShell) Then
strMsg = strMsg & "Failed Creating: "& strTargetDrive & strPShell & vbCrLf
Else
strMsg = strMsg & "Created: " & strTargetDrive & strPShell & vbCrlf
End If
If Not CopyFolder(strSourceDrive & strScripts, strTargetDrive & strScripts) Then
strMsg = strMsg & "Failed to Copy: " & strSourceDrive & strScripts & vbCrLf
Else
strMsg = strMsg & "Copied: " & strSourceDrive & strScripts & vbCrLf
End If
If Not CopyFolder(strSourceDrive & strPShell, strTargetDrive & strPShell) Then
strMsg = strMsg & "Failed to Copy: " & strSourceDrive & strPShell & vbCrLf
Else
strMsg = strMsg & "Copied: " & strSourceDrive & strPShell & vbCrLf
End If
End Function
'-----------
Function CreateFolder(strFolder)
On Error Resume Next
Dim FolderArray, Folder, Path, booErr
FolderArray = Split(strFolder,"\")
For Each Folder In FolderArray
If Path <> "" Then
If Not oFSO.FolderExists(Path) Then
oFSO.CreateFolder(Path)
End If
End If
Path = Path & LCase(Folder) & "\"
If Err.Number <> 0 Then
booErr = True
End If
Next
If booErr Then
CreateFolder = False
Else
CreateFolder = True
End If
End Function
'-----------
Function CopyFolder(strSource, strDest)
On Error Resume Next
If oFSO.FolderExists(strDest) Then
oFSO.DeleteFolder strDest, True
End If
oFSO.CopyFolder strSource, strDest, True
If Err.Number <> 0 Then
CopyFolder = False
Else
CopyFolder = True
End If
End Function
There you go, a bit lacking on the sophistication side of things, but it gets the job done for me. Feel free to modify the script for your own personal file backup needs, it can really help to streamline the process.

