Skip to content

Instantly share code, notes, and snippets.

@figueroadavid
Created February 29, 2024 23:08
Show Gist options
  • Select an option

  • Save figueroadavid/12dee4854fd91df7ad865e5efaa92550 to your computer and use it in GitHub Desktop.

Select an option

Save figueroadavid/12dee4854fd91df7ad865e5efaa92550 to your computer and use it in GitHub Desktop.
Back up the current user's regsitry keys for Putty
function Backup-PuttyKey {
<#
.SYNOPSIS
This script backups a user's registry keys for Putty
.DESCRIPTION
This script backs up the user's registry keys for Putty 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 Putty_<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-PuttyKey -ExportDirectory C:\temp -Overwrite
Directory: C:\temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2/29/2024 4:48 PM 35422 Putty_User1_2024-02-29_164802.reg
#>
[cmdletbinding()]
param(
[parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]$ExportDirectory,
[parameter(ValueFromPipelineByPropertyName)]
[switch]$Overwrite,
[parameter(ValueFromPipelineByPropertyName)]
[switch]$Silent
)
$PuttyPath = 'HKEY_CURRENT_USER\SOFTWARE\SimonTatham'
if (-not (test-Path -Path registry::$PuttyPath)) {
throw ('Unable to find Putty 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 = 'Putty_{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 $PuttyPath, $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