How to encode user info via powershell
Todays code snippet is an example of how to encode user info into a byte array. In this particular scenario, the users details are encoded then injected into the registry to pre-populate a Microsoft Office “userinfo” requirement.
This code also provides a good example of how to create and populate new registry entries, in this case the script executes locally but the same commands could be applied to updating remote registries as required.
# Set our encoding to UTF8 $enc = [system.Text.Encoding]::UTF8 # Store the username from the environment variables $stringUserName = [environment]::UserName # Convert the string username to a byte array $byteUserName = $enc.GetBytes($stringUserName) # Convert the first character of the username to a byte array for use as the initial $byteUserInitial = $enc.GetBytes($stringUserName[0]) # Create the correct registry structure if it does not exist # Check for path and if it doesn't exist create it If (!(test-path HKCU:\Software\Microsoft\Office)) { $result = New-Item -force -Path HKCU:\Software\Microsoft -Name Office } # Check for path and if it doesn't exist create it If (!(test-path HKCU:\Software\Microsoft\Office\11.0)) { $result = New-Item -force -Path HKCU:\Software\Microsoft\Office -Name 11.0 } # Check for path and if it doesn't exist create it If (!(test-path HKCU:\Software\Microsoft\Office\11.0\Common)) { $result = New-Item -force -Path HKCU:\Software\Microsoft\Office\11.0 -Name Common } # Check for path and if it doesn't exist create it If (!(test-path HKCU:\Software\Microsoft\Office\11.0\Common\UserInfo)) { $result = New-Item -force -Path HKCU:\Software\Microsoft\Office\11.0\Common -Name UserInfo } # Once the structure is built - create a new property using the byte array of the userName $result = New-ItemProperty -force -Path HKCU:\Software\Microsoft\Office\11.0\Common\UserInfo -Name UserName -PropertyType Binary -Value $byteUserName # Create a new property using the byte array of the username $result = New-ItemProperty -force -Path HKCU:\Software\Microsoft\Office\11.0\Common\UserInfo -Name UserInitials -PropertyType Binary -Value $byteUserInitial