Vihaan Reyansh
Vihaan Reyansh

Reputation: 89

Powershell script to remotely change registry setting based on version or installation path of program

I have a Powershell script that remotely adds a registry setting to computers on the network depending on which version of MS Office they have installed. At the moment, the script looks in c:\Program Files\Microsoft Office, then checks to see there is either an Office15 or Office16 folder than applies the appropriate reg entry. The problem is that I've discovered that some assets on the network also have the 32 bit version of MS Office, so their installations are going to be in c:\Program Files (x86)\Microsoft Office (\Office15 or \Office16), even though the reg entries added will be the same.

At the moment, I can't figure out how to make the script look for the two possible installation locations it could be in first (Program Files (x86)\Microsoft Office or Program Files\Microsoft Office), before determining what to do next (If ($Ver.name -Contains 'Office16')) etc.

The script:

$Asset = Read-Host "Please enter the asset number of the machine"
$User = Read-Host "Please enter the username of the users account"
$SID = Get-ADUser -Identity $User

If (Test-Connection $Asset -ErrorAction SilentlyContinue){
    $Ver = Get-ChildItem \\$Asset\c$\Program Files\Microsoft Office
    If ($Ver.name -Contains 'Office16') {
                Write-Host ""
                $RegPath = \\$($Asset)\HKEY_USERS\$($SID.sid)\Software\Microsoft\Office\16.0\Outlook\Preferences
        REG ADD $RegPath /f /v DelegateSentItemsStyle /t REG_DWORD /d 1
    } ElseIf ($Ver.name -Contains 'Office15') {        
                Write-Host ""
                $RegPath = \\$($Asset)\HKEY_USERS\$($SID.sid)\Software\Microsoft\Office\15.0\Outlook\Preferences
        REG ADD $RegPath /f /v DelegateSentItemsStyle /t REG_DWORD /d 1
    } Else {
       Write-Host ""
       Write-Host "Unable to determine the version of Office installed." -ForegroundColor Red
    }
} Else {
    Write-Host ""
    Write-Host "Unable to contact the machine. Please ensure you have enter the correct asset number" -ForegroundColor Green
}
pause

Any ideas appreciated.

Cheers.

Upvotes: 2

Views: 968

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60220

Get-ChildItem can look into an array of "paths", hence you can store both known paths for Microsoft Office in an array and search for both of them. I've changed the order of the condition statements a bit, I personally find this more straight forward:

$Asset = Read-Host "Please enter the asset number of the machine"
$User = Read-Host "Please enter the username of the users account"
$SID = Get-ADUser -Identity $User

$paths = @(
    "\\$Asset\c$\Program Files (x86)\Microsoft Office"
    "\\$Asset\c$\Program Files\Microsoft Office"
)

If (-not (Test-Connection $Asset -Quiet)) {
    Write-Warning "Unable to contact the machine. Please ensure you have enter the correct asset number"
    break
}

$Ver = Get-ChildItem $paths -ErrorAction SilentlyContinue -ErrorVariable Errors
if(-not $Ver) {
    Write-Warning "Failed to retrieve paths on $Asset"
    $Errors.Exception.Message
    break
}
if($Ver.Name -Contains 'Office16') {
    $RegPath = "\\$Asset\HKEY_USERS\$($SID.sid)\Software\Microsoft\Office\16.0\Outlook\Preferences"
    REG ADD $RegPath /f /v DelegateSentItemsStyle /t REG_DWORD /d 1
}
elseif($Ver.Name -Contains 'Office15') {
    $RegPath = "\\$Asset\HKEY_USERS\$($SID.sid)\Software\Microsoft\Office\15.0\Outlook\Preferences"
    REG ADD $RegPath /f /v DelegateSentItemsStyle /t REG_DWORD /d 1
}
else {
    Write-Warning "Unable to determine the version of Office installed."
}

Upvotes: 2

Related Questions