The following script will use the user’s AD city and the default gateway to determine if a user is in their home office or visiting an office. If the script determines that the user is not in their home office, it will automatically disable Outlook caching and vice versa for a user that is in their home office.
In order for the script to work, you will need to use the cities listed in AD for your $CurrentLocation switch. Similar to the example below, you will also have to copy your default gateways inside of the switch.
<# .SYNOPSIS Caches a user's mailbox based on machine type or location .NOTES File Name : OutlookCaching.ps1 Author : Jose Espitia Requires : PowerShell V5 Version : Version 1.00 #> if($env:COMPUTERNAME -like "*-D") { # Get Default Gateway $CurrentDefaultGateway = (Get-WmiObject Win32_NetworkAdapterConfiguration) | foreach { $_.DefaultIPGateway } # Find Machine's Location $CurrentLocation = switch ($CurrentDefaultGateway) { 10.101.1.1 {"Orlando"} 10.102.1.1 {"Miami"} 10.103.1.1 {"Atlanta"} 10.104.1.1 {"New York City"} 10.105.1.1 {"Denver"} 10.106.1.1 {"Austin"} 10.107.1.1 {"Los Angeles"} 10.108.1.1 {"Las Vegas"} 10.109.1.1 {"Seattle"} } # Get User's Info $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-Object System.DirectoryServices.DirectoryEntry $objSearcher.Filter = "(&(objectCategory=User)(SAMAccountName=$env:USERNAME))" $objSearcher.SearchScope = "Subtree" $obj = $objSearcher.FindOne() $UsersHome = $obj.Properties["l"] # Determine if user is in their home office if($UsersHome -match $CurrentLocation) { cmd /c REG ADD "HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\16.0\Outlook\Cached Mode" /v Enable /t REG_DWORD /d 1 /f } else { cmd /c REG ADD "HKEY_CURRENT_USER\Software\Policies\Microsoft\Office\16.0\Outlook\Cached Mode" /v Enable /t REG_DWORD /d 0 /f } }