PADT – Registry

PowerShell Application Deployment Toolkit is a tool very powerful to manage the registry:

  • Adding, modifying, deleting registry keys and registry values
  • Adding HKEY CURRENT USER registry entries for all existing user profiles + default user, to avoid Active Setup
  • Modifying the value of environment variables (for example the PATH environment variable)

 

1. Import REG File

To import MyRegistryChange.reg (located in the “Files” folder)

## Import MyRegistryChange.reg
Write-Log "Import MyRegistryChange.reg (HKLM entries)"
Execute-Process -Path "reg.exe" -Parameters "import `"$dirFiles\MyRegistryChange.reg`""

 

2. Adding HKCU for All Existing User Profiles

Allowing implementing HKCU registry for all existing user profiles + default user, to avoid Active Setup.

## Add HKCU registry (for all existing profiles + default user)
Write-Log "Add HKCU registry for all existing profiles + default user"
[scriptblock]$HKCURegistrySettings = {
Set-RegistryKey -Key 'HKCU:SOFTWARE\Manufacturer\Software\Component' -Name 'EntryName1' -value 'EntryValue1' -Type Dword -SID $UserProfile.SID
Set-RegistryKey -Key 'HKCU:SOFTWARE\Manufacturer\Software\Component' -Name 'EntryName2' -value 'EntryValue2' -Type Dword -SID $UserProfile.SID
}
Invoke-HKCURegistrySettingsForAllUsers -RegistrySettings $HKCURegistrySettings

Options: ‘Binary’, ‘DWord’, ‘ExpandString’, ‘MultiString’, ‘None’, ‘QWord’, ‘String’, ‘Unknown’. Default: String.

 

3. PATH Environment Variable

## 
## This function allows to add a folder in the PATH environment variable if not already existing
##
## Then, call the function with the folder to add as parameter
##
 
## Function
Function global:ADD-PATH() {
    [Cmdletbinding()]
    param ( 
        [parameter(Mandatory=$True, ValueFromPipeline=$True, Position=0)]
        [String[]]$AddedFolder
    )
 
    # Get the current PATH from the environment variable in the registry.
    $OldPath=(Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).Path
 
    # Check if the parameter is an existing folder
    IF (!(TEST-PATH $AddedFolder)) { Write-Host 'Folder does not exist. PATH Unchanged' }
 
    ELSE {
 
        # Check if the new folder is already in the PATH
        IF ($OldPath | Select-String -SimpleMatch $AddedFolder) { Write-Host 'Folder already in the PATH. PATH Unchanged' }
 
        # Else set the new PATH
        ELSE {
            $NewPath=$OldPath+';'+$AddedFolder
            Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH –Value $newPath
            Write-Host 'Folder added in PATH'
            Write-Host "New path value is $NewPath"
        }
    }
}
 
## Call 
ADD-PATH('C:\Workdir')