From 36e3efdd8f50a0faf597547a7c87a3b1a406c231 Mon Sep 17 00:00:00 2001 From: freedbygrace Date: Thu, 26 Sep 2024 17:08:32 -0400 Subject: [PATCH] Create Set-RegistryValueForAllUsers.ps1 --- Set-RegistryValueForAllUsers.ps1 | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Set-RegistryValueForAllUsers.ps1 diff --git a/Set-RegistryValueForAllUsers.ps1 b/Set-RegistryValueForAllUsers.ps1 new file mode 100644 index 0000000..17fdd41 --- /dev/null +++ b/Set-RegistryValueForAllUsers.ps1 @@ -0,0 +1,79 @@ +$UserProfilePropertyList = New-Object -TypeName 'System.Collections.Generic.List[System.Object]' + $UserProfilePropertyList.Add('*') + $UserProfilePropertyList.Add(@{Name = 'NTAccount'; Expression = {Try {[System.Security.Principal.SecurityIdentifier]::New($_.SID).Translate([System.Security.Principal.NTAccount]).Value} Catch {$Null}}}) + +$UserProfileList = Get-CIMInstance -Namespace 'Root\CIMv2' -ClassName 'Win32_UserProfile' | Where-Object {($_.Special -eq $False)} | Select-Object -Property ($UserProfilePropertyList) + +$UserProfileListCount = ($UserProfileList | Measure-Object).Count + +For ($UserProfileListIndex = 0; $UserProfileListIndex -lt $UserProfileListCount; $UserProfileListIndex++) + { + $UserProfile = $UserProfileList[$UserProfileListIndex] + + $RegistryTableList = New-Object -TypeName 'System.Collections.Generic.List[System.Collections.Specialized.OrderedDictionary]' + $RegistryTableList.Add([Ordered]@{Enabled = $True; KeyPath = "HKEY_USERS\$($UserProfile.SID)\Software\Microsoft\Office\16.0\Outlook\Options\General"; ValueName = 'HideNewOutlookToggle'; DesiredValue = '1'; ValueKind = [Microsoft.Win32.RegistryValueKind]::DWord}) + #$RegistryTableList.Add([Ordered]@{Enabled = $True; KeyPath = "HKEY_USERS\$($UserProfile.SID)\Software\Microsoft\Office\16.0\Outlook\Options\General"; ValueName = 'HideNewOutlookToggle'; DesiredValue = '1'; ValueKind = [Microsoft.Win32.RegistryValueKind]::DWord}) + + $RegistryTableListCount = $RegistryTableList.Count + + $RegistryTableListCounter = 1 + + For ($RegistryTableListIndex = 0; $RegistryTableListIndex -lt $RegistryTableListCount; $RegistryTableListIndex++) + { + Try + { + $RegistryTable = $RegistryTableList[$RegistryTableListIndex] + + Switch ($RegistryTable.Enabled) + { + {($_ -eq $True)} + { + $RegistryValue = [Microsoft.Win32.Registry]::GetValue($RegistryTable.KeyPath, $RegistryTable.ValueName, 'ValueDoesNotExist') + + Switch (($RegistryValue -ieq 'ValueDoesNotExist') -or ($RegistryValue -ine $RegistryTable.DesiredValue)) + { + {($_ -eq $True)} + { + $LogMessage = "Attempting to perform registry configuration $($RegistryTableListCounter) of $($RegistryTableListCount). Please Wait..." + Write-Verbose -Message ($LogMessage) -Verbose + + ForEach ($RegistryTableKVP In $RegistryTable.GetEnumerator()) + { + Switch ($RegistryTableKVP.Key) + { + {($_ -inotin @('Enabled'))} + { + $LogMessage = "$($RegistryTableKVP.Key): $($RegistryTableKVP.Value)" + Write-Verbose -Message ($LogMessage) -Verbose + } + } + } + + $Null = [Microsoft.Win32.Registry]::SetValue($RegistryTable.KeyPath, $RegistryTable.ValueName, $RegistryTable.DesiredValue, $RegistryTable.ValueKind) + } + } + } + } + } + Catch + { + $ExceptionPropertyDictionary = New-Object -TypeName 'System.Collections.Specialized.OrderedDictionary' + $ExceptionPropertyDictionary.Message = $_.Exception.Message + $ExceptionPropertyDictionary.Category = $_.Exception.ErrorRecord.FullyQualifiedErrorID + $ExceptionPropertyDictionary.Script = Try {[System.IO.Path]::GetFileName($_.InvocationInfo.ScriptName)} Catch {$Null} + $ExceptionPropertyDictionary.LineNumber = $_.InvocationInfo.ScriptLineNumber + $ExceptionPropertyDictionary.LinePosition = $_.InvocationInfo.OffsetInLine + $ExceptionPropertyDictionary.Code = $_.InvocationInfo.Line.Trim() + + ForEach ($ExceptionProperty In $ExceptionPropertyDictionary.GetEnumerator()) + { + $LogMessage = "$($ExceptionProperty.Key): $($ExceptionProperty.Value)" + Write-Warning -Message ($LogMessage) -Verbose + } + } + Finally + { + $RegistryTableListCounter++ + } + } + }