Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Last active February 29, 2024 23:06
Show Gist options
  • Select an option

  • Save figueroadavid/350e10670cdb84b5d937e4d7cdc88041 to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/350e10670cdb84b5d937e4d7cdc88041 to your computer and use it in GitHub Desktop.
Back up the registry keys for WinSCP
function Backup-WinSCPKey {
<#
.SYNOPSIS
This script backups a user's registry keys for WinSCP
.DESCRIPTION
This script backs up the user's registry keys for WinSCP to a specified directory.
.NOTES
This script does not require any special rights
.PARAMETER ExportDirectory
This is the directory where the exported file will be created.
The name of the file is WinSCP_<samaccountname>_<timestamp>.reg
.PARAMETER Overwrite
If the export file exists, this adds the /y switch to the reg export so that it will overwrite the file.
.PARAMETER Silent
This tells the script to not return the file object of the created export file
.EXAMPLE
PS C:\> Backup-WinSCPKey -ExportDirectory C:\temp -Overwrite
Directory: C:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/29/2024 4:48 PM 35422 WinSCP_User1_2024-02-29_164802.reg
#>
[cmdletbinding()]
param(
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$ExportDirectory,
[parameter(ValueFromPipelineByPropertyName)]
[switch]$Overwrite,
[parameter(ValueFromPipelineByPropertyName)]
[switch]$Silent
)
$WinSCPPath = 'HKEY_CURRENT_USER\SOFTWARE\Martin Prikryl'
if (-not (test-Path -Path registry::$WinSCPPath)) {
throw ('Unable to find WinSCP key in HKCU, exiting')
}
$ExportPath = [System.IO.Path]::GetFullPath($ExportDirectory)
if (Test-Path -Path $ExportPath) {
$thisPath = Get-Item -Path $ExportPath
if ($thisPath -is [System.IO.DirectoryInfo]) {
Write-Verbose -Message ('$ExportDirectory ({0}) is a directory, continuing' -f $ExportPath)
}
else {
Throw ('The provided $ExportDirectory ({0}) is not a valid path, unable to continue' -f $ExportPath)
}
}
else {
throw ('$ExportDirectory ({0}) does not exist, unable to continue' -f $ExportDirectory)
}
$ExportFileName = 'WinSCP_{0}_{1}.reg' -f $env:USERNAME, [datetime]::Now.ToString('yyyy-MM-dd_HHmmss')
$FilePath = [system.io.path]::Combine($ExportDirectory, $ExportFileName)
$StartProcInfo = [System.Diagnostics.ProcessStartInfo]::new('reg.exe')
$StartProcInfo.CreateNoWindow = $true
$StartProcInfo.LoadUserProfile = $true
$StartProcInfo.UseShellExecute = $false
$StartProcInfo.RedirectStandardError = $true
$StartProcInfo.RedirectStandardOutput = $true
$StartProcInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
$ArgList = 'export "{0}" "{1}"' -f $WinSCPPath, $FilePath
if ($Overwrite) {
$StartProcInfo.Arguments = ('{0} /y' -f $ArgList)
}
else {
$StartProcInfo.Arguments = $ArgList
}
$Proc = [System.Diagnostics.Process]::new()
$Proc.StartInfo = $StartProcInfo
[void]$Proc.Start()
$Proc.WaitForExit()
$MyExitCode = $Proc.ExitCode
switch ($MyExitCode) {
0 {
Write-Verbose -Message 'Successful'; break
if (-not $Silent) {
}
}
1 { Write-Warning -Message ($Proc.StandardError.ReadToEnd()); break }
2 { Write-Warning -Message 'Successful, but something is different{0}StdOut:{0}{1}{0}StdErr:{0}{2}' -f "`r`n", $Proc.StandardOutput.ReadToEnd(), $Proc.StandardError.ReadToEnd() }
}
if (-not $Silent) {
Get-Item -Path $FilePath
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment