Sunday, 15 May 2016

Migrating Outlook Profile to Office 365

We had a migration from Hosted Exchange to Office 365 and i was tasked with automating the local Outloook profile migration for Users:

I Created a GUI utility using powershell which would allow users to create an Office365 Profile and set it as default, I prepared PRF files for each version of office and an autodiscover.xml to be used for local autodiscover and uploaded them to a hosted site:

The PRF file to set settings for Office 365 are hard to find: i used the below entries:


;Automatically generated PRF file from the Microsoft Office Customization and Installation Wizard

; **************************************************************
; Section 1 - Profile Defaults
; **************************************************************

[General]
Custom=1
ProfileName=%UserName%-O365
DefaultProfile=Yes
OverwriteProfile=Yes
ModifyDefaultProfileIfPresent=false

; **************************************************************
; Section 2 - Services in Profile
; **************************************************************

[Service List]
;ServiceX=Microsoft Outlook Client
ServiceEGS1=Exchange Global Section
Service1=Microsoft Exchange Server

;***************************************************************
; Section 3 - List of internet accounts
;***************************************************************

[Internet Account List]

;***************************************************************
; Section 4 - Default values for each service.
;***************************************************************

;[ServiceX]
;FormDirectoryPage=
;-- The URL of Exchange Web Services Form Directory page used to create Web forms.
;WebServicesLocation=
;-- The URL of Exchange Web Services page used to display unknown forms.
;ComposeWithWebServices=
;-- Set to true to use Exchange Web Services to compose forms.
;PromptWhenUsingWebServices=
;-- Set to true to use Exchange Web Services to display unknown forms.
;OpenWithWebServices=
;-- Set to true to prompt user before opening unknown forms when using Exchange Web Services.


[ServiceEGS1]
MailboxName=%UserName%@company.com
HomeServer=outlook.office365.com
AccountName=%UserName%@company.com
ConfigFlags=0x00000100
RPCoverHTTPflags=0x002f
RPCProxyServer=outlook.office365.com
RPCProxyPrincipalName=msstd:outlook.com
RPCProxyAuthScheme=0x0001
NetworkLogonSecurity=0x8000F001

[Service1]
OverwriteExistingService=No
UniqueService=No
MailboxName=%UserName%@company.com
HomeServer=outlook.office365.com
OfflineAddressBookPath=%USERPROFILE%\local settings\application data\microsoft\outlook\
OfflineFolderPathAndFilename=%USERPROFILE%\local settings\application data\microsoft\outlook\O365-outlook.ost

;***************************************************************
; Section 5 - Values for each internet account.
;***************************************************************

;***************************************************************
; Section 6 - Mapping for profile properties
;***************************************************************

[Microsoft Exchange Server]
ServiceName=MSEMS
MDBGUID=5494A1C0297F101BA58708002B2A2517
MailboxName=PT_STRING8,0x6607
HomeServer=PT_STRING8,0x6608
OfflineAddressBookPath=PT_STRING8,0x660E
OfflineFolderPathAndFilename=PT_STRING8,0x6610

[Exchange Global Section]
SectionGUID=13dbb0c8aa05101a9bb000aa002fc45a
MailboxName=PT_STRING8,0x6607
HomeServer=PT_STRING8,0x6608
ConfigFlags=PT_LONG,0x6601
RPCoverHTTPflags=PT_LONG,0x6623
RPCProxyServer=PT_UNICODE,0x6622
RPCProxyPrincipalName=PT_UNICODE,0x6625
RPCProxyAuthScheme=PT_LONG,0x6627
AccountName=PT_UNICODE,0x6620
NetworkLogonSecurity=PT_LONG,0x6619

[Microsoft Mail]
ServiceName=MSFS
ServerPath=PT_STRING8,0x6600
Mailbox=PT_STRING8,0x6601
Password=PT_STRING8,0x67f0
RememberPassword=PT_BOOLEAN,0x6606
ConnectionType=PT_LONG,0x6603
UseSessionLog=PT_BOOLEAN,0x6604
SessionLogPath=PT_STRING8,0x6605
EnableUpload=PT_BOOLEAN,0x6620
EnableDownload=PT_BOOLEAN,0x6621
UploadMask=PT_LONG,0x6622
NetBiosNotification=PT_BOOLEAN,0x6623
NewMailPollInterval=PT_STRING8,0x6624
DisplayGalOnly=PT_BOOLEAN,0x6625
UseHeadersOnLAN=PT_BOOLEAN,0x6630
UseLocalAdressBookOnLAN=PT_BOOLEAN,0x6631
UseExternalToHelpDeliverOnLAN=PT_BOOLEAN,0x6632
UseHeadersOnRAS=PT_BOOLEAN,0x6640
UseLocalAdressBookOnRAS=PT_BOOLEAN,0x6641
UseExternalToHelpDeliverOnRAS=PT_BOOLEAN,0x6639
ConnectOnStartup=PT_BOOLEAN,0x6642
DisconnectAfterRetrieveHeaders=PT_BOOLEAN,0x6643
DisconnectAfterRetrieveMail=PT_BOOLEAN,0x6644
DisconnectOnExit=PT_BOOLEAN,0x6645
DefaultDialupConnectionName=PT_STRING8,0x6646
DialupRetryCount=PT_STRING8,0x6648
DialupRetryDelay=PT_STRING8,0x6649

[Personal Folders]
ServiceName=MSPST MS
Name=PT_STRING8,0x3001
PathAndFilenameToPersonalFolders=PT_STRING8,0x6700 
RememberPassword=PT_BOOLEAN,0x6701
EncryptionType=PT_LONG,0x6702
Password=PT_STRING8,0x6703

[Unicode Personal Folders]
ServiceName=MSUPST MS
Name=PT_UNICODE,0x3001
PathAndFilenameToPersonalFolders=PT_STRING8,0x6700 
RememberPassword=PT_BOOLEAN,0x6701
EncryptionType=PT_LONG,0x6702
Password=PT_STRING8,0x6703

[Outlook Address Book]
ServiceName=CONTAB

[LDAP Directory]
ServiceName=EMABLT
ServerName=PT_STRING8,0x6600
UserName=PT_STRING8,0x6602
UseSSL=PT_BOOLEAN,0x6613
UseSPA=PT_BOOLEAN,0x6615
EnableBrowsing=PT_BOOLEAN,0x6622
DisplayName=PT_STRING8,0x3001
ConnectionPort=PT_STRING8,0x6601
SearchTimeout=PT_STRING8,0x6607
MaxEntriesReturned=PT_STRING8,0x6608
SearchBase=PT_STRING8,0x6603
CheckNames=PT_STRING8,0x6624
DefaultSearch=PT_LONG,0x6623

[Microsoft Outlook Client]
SectionGUID=0a0d020000000000c000000000000046
FormDirectoryPage=PT_STRING8,0x0270
WebServicesLocation=PT_STRING8,0x0271
ComposeWithWebServices=PT_BOOLEAN,0x0272
PromptWhenUsingWebServices=PT_BOOLEAN,0x0273
OpenWithWebServices=PT_BOOLEAN,0x0274
CachedExchangeMode=PT_LONG,0x041f
CachedExchangeSlowDetect=PT_BOOLEAN,0x0420

[Personal Address Book]
ServiceName=MSPST AB
NameOfPAB=PT_STRING8,0x001e3001
PathAndFilename=PT_STRING8,0x001e6600
ShowNamesBy=PT_LONG,0x00036601

; ************************************************************************
; Section 7 - Mapping for internet account properties.  DO NOT MODIFY.
; ************************************************************************

[I_Mail]
AccountType=POP3
;--- POP3 Account Settings ---
AccountName=PT_UNICODE,0x0002
DisplayName=PT_UNICODE,0x000B
EmailAddress=PT_UNICODE,0x000C
;--- POP3 Account Settings ---
POP3Server=PT_UNICODE,0x0100
POP3UserName=PT_UNICODE,0x0101
POP3UseSPA=PT_LONG,0x0108
Organization=PT_UNICODE,0x0107
ReplyEmailAddress=PT_UNICODE,0x0103
POP3Port=PT_LONG,0x0104
POP3UseSSL=PT_LONG,0x0105
; --- SMTP Account Settings ---
SMTPServer=PT_UNICODE,0x0200
SMTPUseAuth=PT_LONG,0x0203
SMTPAuthMethod=PT_LONG,0x0208
SMTPUserName=PT_UNICODE,0x0204
SMTPUseSPA=PT_LONG,0x0207
ConnectionType=PT_LONG,0x000F
ConnectionOID=PT_UNICODE,0x0010
SMTPPort=PT_LONG,0x0201
SMTPSecureConnection=PT_LONG,0x020A
ServerTimeOut=PT_LONG,0x0209
LeaveOnServer=PT_LONG,0x1000

[IMAP_I_Mail]
AccountType=IMAP
;--- IMAP Account Settings ---
AccountName=PT_UNICODE,0x0002
DisplayName=PT_UNICODE,0x000B
EmailAddress=PT_UNICODE,0x000C
;--- IMAP Account Settings ---
IMAPServer=PT_UNICODE,0x0100
IMAPUserName=PT_UNICODE,0x0101
IMAPUseSPA=PT_LONG,0x0108
Organization=PT_UNICODE,0x0107
ReplyEmailAddress=PT_UNICODE,0x0103
IMAPPort=PT_LONG,0x0104
IMAPUseSSL=PT_LONG,0x0105
; --- SMTP Account Settings ---
SMTPServer=PT_UNICODE,0x0200
SMTPUseAuth=PT_LONG,0x0203
SMTPAuthMethod=PT_LONG,0x0208
SMTPUserName=PT_UNICODE,0x0204
SMTPUseSPA=PT_LONG,0x0207
ConnectionType=PT_LONG,0x000F
ConnectionOID=PT_UNICODE,0x0010
SMTPPort=PT_LONG,0x0201
SMTPSecureConnection=PT_LONG,0x020A
ServerTimeOut=PT_LONG,0x0209
CheckNewImap=PT_LONG,0x1100
RootFolder=PT_UNICODE,0x1101
Account=PT_UNICODE,0x0002
HttpServer=PT_UNICODE,0x0100
UserName=PT_UNICODE,0x0101
Organization=PT_UNICODE,0x0107
UseSPA=PT_LONG,0x0108
TimeOut=PT_LONG,0x0209
Reply=PT_UNICODE,0x0103
EmailAddress=PT_UNICODE,0x000C
FullName=PT_UNICODE,0x000B
Connection Type=PT_LONG,0x000F
ConnectOID=PT_UNICODE,0x0010
The Outlook Office 365 migration utility was created to assist with the creation of a local office 365 outlook profile for FICO users. When run, the below screen shows up,



When the user clicks 'Migrate Outlook to Office365' It makes the following changes:
1: Downloads an Autodiscover file to the folder %AppData%\FICO-AD


2: Downloads a .PRF file (PRF varies by installed Office version) to the folder %UserProfile%\Documents\Office365-PRF



3: Creates the following registry keys:
      

Registry Key PathValue NameValueTypeAction
HKCU:\Software\Microsoft\Office\*.0\Outlook\cached modesyncwindowsetting3DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverExcludeHttpRedirect0DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverExcludeHttpsAutodiscoverDomain1DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverExcludeHttpsRootDomain1DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverExcludeScpLookup1DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverExcludeSrvLookup1DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverExcludeSrvRecord1DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverfico.comStringCreated
HKCU:\Software\Microsoft\Office\*.0\Outlook\AutoDiscoverPreferLocalXML1DWORD'Created
HKCU:\Software\Microsoft\Office\*.0\Outlook\SetupImportPRFStringCreated
HKCU:\Software\FICO%UserName%-O365
StringCreated

4: And deletes the following Registry Value:
      
Registry Key PathValue NameValueTypeAction
HKCU:\Software\Microsoft\Office\*.0\Outlook\SetupFirst-Run
StringDeleted





5: A log file is created and displayed in a textbox within the utility itself, a local copy of the log file can be found at :
%temp%/FICO_O365_Profile_format `"yyyyMMdd_hhmmsstt`>.log

6: Once the utility is done running successfully for the first time it Displays a list of changes made and the user is prompted to restart Outlook as shown below:



7: When Outlook is restarted, the it responds to the first-run key being deleted by attempting to configure the current user's profile again with the .PRF file specified in the Registry value:

HKCU:\Software\Microsoft\Office\15.0\Outlook\Setup\ImportPRF

8: When successful, Outlook will prompt for the logged in user's credentials by presenting a authentication box,
The username will need to be the user's email address and password will be CORP password :



9: Once Authentication is successful and the user's Identity is verified a Outlook Profile (%UserName%-O365) is created, the existing Outlook profile is left untouched.

Re-running the utility:


When a user attempts to re-run the utility he will be prompted to delete the HKCU\Software\company key in order to allow it to re-run :


if you'd like to re-run the utility, please perform the following steps:

1: Open Mail in Control Panel and click 'Show Profiles' select the newly created profile and click 'remove'.



2: Delete the Registry Key HKCU:\Software\company



The Code for the Action 'Migrate Outlook to Office 365' is as follows:


#####Logging functions######
  $Logfile = "${Env:TEMP}\FICO_O365_Profile_$(get-date -format `"yyyyMMdd_hhmmsstt`").log"
  Function LogWrite($string, $color)
  {
   if ($Color -eq $null) { $color = "white" }
   write-host $string -foregroundcolor $color
   $string | out-file -Filepath $logfile -append
  }
  
  function CreateRegistryKey([string]$keyPath)
  {
   if (-not (Test-Path $keyPath))
   {
    New-Item -Path "$keyPath";
   }
  }
  
  function CreateRegistryKeys([string]$rootKey)
  {
   [string]$keyPath = [String]::Format("{0}:", $rootKey);
   
   foreach ($keyName in $args)
   {
    [string]$keyPath = [String]::Format("{0}\{1}", $keyPath, $keyName);
    CreateRegistryKey $keyPath;
   }
  }
  
  function DeleteRegistryKey([string]$rootKey, [string]$keyName)
  {
   [string]$keyPath = [String]::Format("{0}:\{1}", $rootKey, $keyName);
   
   if (Test-Path $keyPath)
   {
    Remove-Item -Path "$keyPath" -Recurse;
   }
  }
  
  function Test-RegistryEntry ([string]$key, [string]$name)
  {
   Get-ItemProperty -Path $key -Name $name -ErrorAction SilentlyContinue | Out-Null;
   return $?;
  }
  function Read-RegistryEntry ([string]$key, [string]$name)
  {
   if (Test-RegistryEntry $key $name)
   {
    return (Get-ItemProperty -Path $key -Name $name).$name;
   }
   else
   {
    return '';
   }
  }
  
  function Set-RegistryEntry ([string]$key, [string]$name, [string]$value, [string]$type = 'String')
  {
   if (-not (Test-RegistryEntry $key $name))
   {
    if (($type -eq 'DWORD') -or ($type -eq 'String'))
    {
     New-ItemProperty -Path $key -Name $name -PropertyType $type -Value $value;
    }
   }
   else
   {
    Set-ItemProperty -Path $key -Name $name -Value $value;
    Write-Output "The Entry [$key/$name = $value] is modified.";
   }
  }
  
  function Rename-RegistryEntry ([string]$key, [string]$name, [string]$newName)
  {
   if (Test-RegistryEntry $key $name)
   {
    Rename-ItemProperty -Path $key -Name $name -NewName $newName;
   }
  }
  
  function Remove-RegistryEntry ([string]$key, [string]$name)
  {
   if (Test-RegistryEntry $key $name)
   {
    Remove-ItemProperty -Path $key -Name $name;
   }
  }
  
  function Download-AutoD ()
  {
   $ficoaddir = "${Env:APPDATA}\FICO-AD"
   if (!(Test-Path -Path $ficoaddir))
   {
    New-Item -ItemType directory -Path $ficoaddir
   }
   $ficoadloc = "${Env:APPDATA}\FICO-AD\autodiscover.xml"
   $adurl = "https://autodiscover.xmlurl.xml"
   $wc = New-Object System.Net.WebClient
   $wc.DownloadFile($adurl, $ficoadloc)
   if ($?)
   {
    LogWrite "autodiscover.xml downloaded to $ficoadloc" green
   }
   else
   {
    Write-Error "Download of autodisover.xml failed"
    LogWrite "Download of autodiscover.xml failed" -color red
   }
  }
  #####################End of function###############
  #get current username
  $suname = [Security.Principal.WindowsIdentity]::GetCurrent().Name
  $CP_USER_NAME = $suname.replace("$ENV:USERDOMAIN\", "")
  #Concatenate it with nonsensical abbrv so that the place holder key is username-abbrv
  $holder = $CP_USER_NAME + '-O365'
  ### Need to Check for placeholder and exit if the value exists ###
  $placeholder = "HKCU:\Software\FICO\$holder"
  
 
  function Write-TextBox
  {
   param ([string]$text)
   $richtextbox1.Text += "$text`n"
  }
  
  $testdone = Test-Path $placeholder
  If ($testdone -eq $true)
  {
   #Exit ##because script has run previously.
   LogWrite "This script has been run previously, HKCU\Software\FICO detected, delete this key if you want to re-run this script" red
   Write-Error "This script has been run previously, HKCU\Software\FICO detected, delete this key if you want to re-run this script"
   
  }
  ElseIf ($testdone -eq $false)
  {
   $prfloc = "${Env:USERPROFILE}\Documents\Office365-PRF"
   if (!(Test-Path -Path $prfloc))
   {
    New-Item -ItemType directory -Path $prfloc
   }
   Download-AutoD
   #if the Installed outlookversion is 2010
   If (Test-Path HKCU:'\Software\Microsoft\Office\14.0\Outlook\Setup')
   {
    if (Test-Path "${Env:ProgramFiles(x86)}\Microsoft Office\Office14\Outlook.exe")
    {
     $url = "https://prffileurl.prf"
     $localPath = "$prfloc\Office365-2010-x86.PRF"
     $wc = New-Object System.Net.WebClient
     $wc.DownloadFile($url, $localPath)
     if ($?)
     {
      LogWrite "Office 2010 x86 installed" red
      $regkeypath = "HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover"
      $valuevalue = "fico.com"
      $value1 = (Get-ItemProperty $regkeypath).$valuevalue -eq $null
      If ($value1 -eq $False)
      {
       Remove-RegistryEntry $regkeypath $valuevalue
       LogWrite "fico.com registry value deleted" yellow
      }
      Else
      {
       LogWrite "The autodiscover value fico.com does not exist" red
      }
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\cached mode' 'syncwindowsetting' '3' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeHttpRedirect' '0' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeHttpsAutodiscoverDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeHttpsRootDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeScpLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeSrvLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeSrvRecord' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'fico.com' "${Env:APPDATA}\FICO-AD\autodiscover.xml"
      LogWrite "Set autodiscover entry" green
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'PreferLocalXML' '1' 'DWORD'
      LogWrite "Set PreferLocalXML" green
      Remove-RegistryEntry "HKCU:\Software\Microsoft\Office\14.0\Outlook\Setup" 'First-Run'
      LogWrite "Deleted First-run key" yellow
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\Setup' 'ImportPRF' "${Env:USERPROFILE}\Documents\Office365-PRF\office365-2010-x86.PRF"
      LogWrite "ImportPRF key applied" yellow
      New-Item -Path HKCU:\Software\FICO -Name $holder -Value "Default Value" -Force -ErrorAction SilentlyContinue
      New-ItemProperty -Path $placeholder -Name "Done" -PropertyType "String" -Value 'yes' -ErrorAction SilentlyContinue
      LogWrite "Created Placeholder at HKCU:\Software\FICO" yellow
     }
     else
     {
      Write-Error "Download of PRF file failed"
      LogWrite "Download of PRF file failed" -color red
     }
    }
    
    elseif (Test-Path "${Env:ProgramFiles}\Microsoft Office\Office14\Outlook.exe")
    {
     $url = "https://prffileurl.prf"
     $localPath = "$prfloc\Office365-2010-x64.PRF"
     $wc = New-Object System.Net.WebClient
     $wc.DownloadFile($url, $localPath)
     if ($?)
     {
      LogWrite "Office 2010 x64 installed" red
      $regkeypath = "HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover"
      $valuevalue = "fico.com"
      $value1 = (Get-ItemProperty $regkeypath).$valuevalue -eq $null
      If ($value1 -eq $False)
      {
       Remove-RegistryEntry $regkeypath $valuevalue
       LogWrite "fico.com registry value deleted" yellow
      }
      Else
      {
       LogWrite "The autodiscover value fico.com does not exist" red
      }
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\cached mode' 'syncwindowsetting' '3' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeHttpRedirect' '0' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeHttpsAutodiscoverDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeHttpsRootDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeScpLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeSrvLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'ExcludeSrvRecord' '1' 'DWORD'
      
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'fico.com' "${Env:APPDATA}\FICO-AD\autodiscover.xml"
      LogWrite "Set autodiscover entry" green
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\14.0\Outlook\AutoDiscover' 'PreferLocalXML' '1' 'DWORD'
      LogWrite "Set PreferLocalXML" green
      Remove-ItemProperty -Name 'First-Run' -Path "HKCU:\Software\Microsoft\Office\14.0\Outlook\Setup" -ErrorAction SilentlyContinue
      LogWrite "Deleted First-run key" yellow
      Set-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Outlook\Setup' -Name ImportPRF -Value "${Env:USERPROFILE}\Documents\Office365-PRF\office365-2010-x66.PRF" -type String -ErrorAction SilentlyContinue
      LogWrite "ImportPRF key applied" yellow
      New-Item -Path HKCU:\Software\FICO -Name $holder -Value "Default Value" -Force -ErrorAction SilentlyContinue
      New-ItemProperty -Path $placeholder -Name "Done" -PropertyType "String" -Value 'yes' -ErrorAction SilentlyContinue
      LogWrite "Created Placeholder at HKCU:\Software\FICO" yellow
     }
     else
     {
      Write-Error "Download of PRF file failed"
      LogWrite "Download of PRF file failed" -color red
     }
    }
    
   }
   ElseIf ((Test-Path HKCU:'\Software\Microsoft\Office\14.0\Outlook\Setup') -eq $False)
   {
    LogWrite "Office 2010 is not installed" red
   }
   
   #if the Installed outlookversion is 2013
   If (Test-Path HKCU:'\Software\Microsoft\Office\15.0\Outlook\Setup')
   {
    #Set keys for outlook 2013
    Download-AutoD
    LogWrite "Office 2013 exists" blue
    if (Test-Path "${Env:ProgramFiles(x86)}\Microsoft Office\Office15\Outlook.exe")
    {
     $url = "https://prffileurl.prf"
     $localPath = "$prfloc\Office365-2013-x86.PRF"
     $wc = New-Object System.Net.WebClient
     $wc.DownloadFile($url, $localPath)
     if ($?)
     {
      $regkeypath = "HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover"
      $valuevalue = "fico.com"
      $value1 = (Get-ItemProperty $regkeypath).$valuevalue -eq $null
      If ($value1 -eq $False)
      {
       Remove-RegistryEntry $regkeypath $valuevalue
       LogWrite "fico.com registry value deleted" yellow
      }
      Else
      {
       LogWrite "The autodiscover value comany.com does not exist" red
      }
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\cached mode' 'syncwindowsetting' '3' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeHttpRedirect' '0' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeHttpsAutodiscoverDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeHttpsRootDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeScpLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeSrvLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeSrvRecord' '1' 'DWORD'
      
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'fico.com' "${Env:APPDATA}\FICO-AD\autodiscover.xml"
      LogWrite "Set autodiscover entry" green
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'PreferLocalXML' '1' 'DWORD'
      LogWrite "Set PreferLocalXML" green
      Remove-ItemProperty -Name 'First-Run' -Path "HKCU:\Software\Microsoft\Office\15.0\Outlook\Setup" -ErrorAction SilentlyContinue
      LogWrite "Deleted First-run key" yellow
      Set-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Setup' -Name ImportPRF -Value "${Env:USERPROFILE}\Documents\Office365-PRF\office365-2013-x86.PRF" -type String -ErrorAction SilentlyContinue
      LogWrite "ImportPRF key applied" yellow
      New-Item -Path HKCU:\Software\FICO -Name $holder -Value "Default Value" -Force -ErrorAction SilentlyContinue
      New-ItemProperty -Path $placeholder -Name "Done" -PropertyType "String" -Value 'yes' -ErrorAction SilentlyContinue
      LogWrite "Created Placeholder at HKCU:\Software\FICO" yellow
     }
     else
     {
      Write-Error "Download of PRF file failed"
      LogWrite "Download of PRF file failed" -color red
     }
    }
    elseif (Test-Path "${Env:ProgramFiles}\Microsoft Office\Office15\Outlook.exe")
    {
     $url = "https://prffileurl.prf"
     $localPath = "$prfloc\Office365-2013-x64.PRF"
     $wc = New-Object System.Net.WebClient
     $wc.DownloadFile($url, $localPath)
     if ($?)
     {
      $regkeypath = "HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover"
      $valuevalue = "fico.com"
      $value1 = (Get-ItemProperty $regkeypath).$valuevalue -eq $null
      If ($value1 -eq $False)
      {
       Remove-RegistryEntry $regkeypath $valuevalue
       LogWrite "fico.com registry value deleted" yellow
      }
      Else
      {
       LogWrite "The autodiscover value fico.com does not exist" red
      }
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\cached mode' 'syncwindowsetting' '3' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeHttpRedirect' '0' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeHttpsAutodiscoverDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeHttpsRootDomain' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeScpLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeSrvLookup' '1' 'DWORD'
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'ExcludeSrvRecord' '1' 'DWORD'
      
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'fico.com' "${Env:APPDATA}\FICO-AD\autodiscover.xml"
      LogWrite "Set autodiscover entry" green
      Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\15.0\Outlook\AutoDiscover' 'PreferLocalXML' '1' 'DWORD'
      LogWrite "Set PreferLocalXML" green
      Remove-ItemProperty -Name 'First-Run' -Path "HKCU:\Software\Microsoft\Office\15.0\Outlook\Setup" -ErrorAction SilentlyContinue
      LogWrite "Deleted First-run key" yellow
      Set-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Outlook\Setup' -Name ImportPRF -Value "${Env:USERPROFILE}\Documents\Office365-PRF\office365-2013-x64.PRF" -type String -ErrorAction SilentlyContinue
      LogWrite "ImportPRF key applied" yellow
      New-Item -Path HKCU:\Software\FICO -Name $holder -Value "Default Value" -Force -ErrorAction SilentlyContinue
      New-ItemProperty -Path $placeholder -Name "Done" -PropertyType "String" -Value 'yes' -ErrorAction SilentlyContinue
      LogWrite "Created Placeholder at HKCU:\Software\FICO" yellow
     }
     else
     {
      Write-Error "Download of PRF file failed"
      LogWrite "Download of PRF file failed" -color red
     }
    }
   }
   ElseIf ((Test-Path HKCU:'\Software\Microsoft\Office\15.0\Outlook\Setup') -eq $False)
   {
    LogWrite "Office 2013 is not installed" red
   }
   #if the Installed outlookversion is 2016
   $2016installed = Get-WmiObject -Namespace 'root\cimv2\sms' -Class SMS_InstalledSoftware | where { $_.ARPDisplayName -match 'Office Professional Plus 2016' }
   if ($2016installed)
   {
    Download-AutoD
    LogWrite "Office Professional Plus 2016 exists, will not apply PRF because its not supported.. please uninstall it " blue
    $regkeypath = "HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover"
    $valuevalue = "fico.com"
    $value1 = (Get-ItemProperty $regkeypath).$valuevalue -eq $null
    If ($value1 -eq $False)
    {
     Remove-RegistryEntry $regkeypath $valuevalue
     LogWrite "fico.com registry value deleted" yellow
    }
    Else
    {
     LogWrite "The autodiscover value fico.com does not exist" red
    }
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\cached mode' 'syncwindowsetting' '3' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeHttpRedirect' '0' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeHttpsAutodiscoverDomain' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeHttpsRootDomain' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeScpLookup' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeSrvLookup' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeSrvRecord' '1' 'DWORD'
    
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'fico.com' "${Env:APPDATA}\FICO-AD\autodiscover.xml"
    LogWrite "Set autodiscover entry" green
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'PreferLocalXML' '1' 'DWORD'
    LogWrite "Set PreferLocalXML" green
    New-Item -Path HKCU:\Software\FICO -Name $holder -Value "Default Value" -Force -ErrorAction SilentlyContinue
    New-ItemProperty -Path $placeholder -Name "Done" -PropertyType "String" -Value 'yes' -ErrorAction SilentlyContinue
    LogWrite "Created Placeholder at HKCU:\Software\FICO" yellow
   }
   #if the Installed outlookversion is 365 proplus
   $365proplusinstalled = Get-WmiObject -Namespace 'root\cimv2\sms' -Class SMS_InstalledSoftware | where { $_.ARPDisplayName -match 'Office 365' }
   if ($365proplusinstalled)
   {
    Download-AutoD
    LogWrite "Office 365 pro plus installed, will not apply PRF because its not supported, please uninstall it" red
    $regkeypath = "HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover"
    $valuevalue = "fico.com"
    $value1 = (Get-ItemProperty $regkeypath).$valuevalue -eq $null
    If ($value1 -eq $False)
    {
     Remove-RegistryEntry $regkeypath $valuevalue
     LogWrite "fico.com registry value deleted" yellow
    }
    Else
    {
     LogWrite "The autodiscover value fico.com does not exist" red
    }
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\cached mode' 'syncwindowsetting' '3' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeHttpRedirect' '0' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeHttpsAutodiscoverDomain' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeHttpsRootDomain' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeScpLookup' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeSrvLookup' '1' 'DWORD'
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'ExcludeSrvRecord' '1' 'DWORD'
    
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'fico.com' "${Env:APPDATA}\FICO-AD\autodiscover.xml"
    LogWrite "Set autodiscover entry" green
    Set-RegistryEntry 'HKCU:\Software\Microsoft\Office\16.0\Outlook\AutoDiscover' 'PreferLocalXML' '1' 'DWORD'
    LogWrite "Set PreferLocalXML" green
    New-Item -Path HKCU:\Software\FICO -Name $holder -Value "Default Value" -Force -ErrorAction SilentlyContinue
    New-ItemProperty -Path $placeholder -Name "Done" -PropertyType "String" -Value 'yes' -ErrorAction SilentlyContinue
    LogWrite "Created Placeholder at HKCU:\Software\FICO" yellow
   }
   LogWrite "" white
   LogWrite "" white
   LogWrite "" white
   LogWrite "Office 365 Outlook profile set, please restart outlook to sign into office 365" yellow
   LogWrite "if your office 365 profile is not configured please open a ticket with:" yellow
   LogWrite "Company It services at http://URL/" yellow
   
  }
  $dateTimeString = [System.DateTime]::Now.ToString("yyyy.MM.dd hh:mm:ss")
  $richtextbox1.SelectionColor = 'Red'
  $logfilereader = Get-Content $Logfile
  
  foreach ($line in $Logfile)
  {
   $richtextbox1.Text += "`n" + $dateTimeString + " - " + $line + "`n"
  }
  
  <#cd $Env:USERPROFILE
  Clear-Host
  Write-Host -ForegroundColor DarkMagenta "**** GTS" 
  Write-Host "Office365 Outlook setup is complete."
  Write-Host "Please restart Outlook to setup your new account."
  Write-host "You may close this window now"
  $global:ExitCode = 0 #Set the exit code for the Packager
  #>
  #notepad $Logfile
 }

Detect autopilot session

  Ensuring that some apps only install during autopilot is not easily accomplished, you can use the below powershell script as a requiremen...