Powershell - Export all Additional Registry Keys from profile

Written by Ines

Last published at: November 16th, 2023

Steps

Example:

 

Script

 

$export = Read-Host "Enter the path to where you want the CSV exported:"
$tstlogs = New-Item -Path "C:\TST_Logs" -ItemType Directory -force -Confirm:$false
$log = "C:\TST_Logs\events.log"

Start-Transcript $export\log.log

$path = Get-ChildItem -path $export -ErrorAction SilentlyContinue

if ($path -eq $null){
    $date = get-date -format MM-dd-yyyy--HH-mm
    Write-Host "$date | Path $export Not found." | Out-File $log -Append -Encoding ascii
    $newfolder = if (New-Item -Path $export -ItemType Directory -force -Confirm:$false){
        $date = get-date -format MM-dd-yyyy--HH-mm
        Write-Host "$date | Path $export created." | Out-File $log -Append -Encoding ascii
        }
        else {
            $date = get-date -format MM-dd-yyyy--HH-mm
            Write-Host "$date | Error creating folder" | Out-File $log -Append -Encoding ascii}
}
else {
    $date = get-date -format MM-dd-yyyy--HH-mm
    Write-Host "$date | Path $export exists." | Out-File $log -Append -Encoding ascii}


import-module ThinscaleManagement

#connecting to server
$URI = Read-Host "Enter the full server URI"
$creds = Get-Credential -Message "Enter server credentials"

try{Connect-TSTMGMTServer -Uri $URI -Credential $creds
    $time = Get-Date -format MM-dd-yyyy--HH-mm
    Write-Output "$time | Succesfully connected to server."| Out-File $log -Append -Encoding ascii}
  catch{
    $exception = $_.Exception.Message
    $time = Get-Date -format MM-dd-yyyy--HH-mm
    Write-Output "$time | Connection to server failed: $exception"| Out-File $log -Append -Encoding ascii
    Write-Output "$time | THE SCRIPT HAS TERMINATED."| Out-File $log -Append -Encoding ascii
  Exit
  }

  start-sleep -Seconds 2

#fetch profile

$Prname = Read-Host "Enter the profile name"
$json = "C:\TST_Logs\$PRname.json" 

$allPr = Get-TSTMGMTProfiles

if ($profile1 = $allPr | where {$_.ProfileName -like "*$PRname*"}){
Export-TSTMGMTProfile -Profile $profile1 -File $json
$time = Get-Date -format MM-dd-yyyy--HH-mm
    Write-Output "$time | Profile $PRname saved in $json"| Out-File $log -Append -Encoding ascii}

else {
    $time = Get-Date -format MM-dd-yyyy--HH-mm
    Write-Output "$time | Unable to save profile. No profile found or wrong name entered."| Out-File $log -Append -Encoding ascii
    Write-Output "$time | THE SCRIPT HAS TERMINATED."| Out-File $log -Append -Encoding ascii
  Exit}


#Convert JSON to powershell object


if($profile2 = Get-Content $json){
    $time = Get-Date -format MM-dd-yyyy--HH-mm
    Write-Output "$time | json file located, mounting..."| Out-File $log -Append -Encoding ascii
    start-sleep -Seconds 3

$PrOut = $profile2 | ConvertFrom-Json

$Data = $prOut.ProfileData | ConvertFrom-Json

$results = $data.ComputerSettings.RegistrySettings

#export results

$results | export-csv $export\registries.csv -NoTypeInformation


#cleanup

Get-ChildItem -Path $json | remove-item -force -confirm:$false}

else{$time = Get-Date -format MM-dd-yyyy--HH-mm
    Write-Output "$time | Unable to save profile. No profile found or wrong name entered."| Out-File $log -Append -Encoding ascii
    Write-Output "$time | THE SCRIPT HAS TERMINATED."| Out-File $log -Append -Encoding ascii
  Exit}

Stop-transcript