configuring Outlook with powershell

I have made this script to reset outlook profile and configure a new profile. this script is deleting the old profile and creating a new one and launches outlook... I want that after launch of outlook the profile should also be configured automatically.. can anyone suggest how to do it further in this script.

clear
if($process=(get-process 'outlook' -ErrorAction SilentlyContinue))
{
    Write-Host "Outlook is running so close it.." -ForegroundColor Green
    kill($process)
    Write-Host "Outlook is stopped " -ForegroundColor Green
}

$reg="HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles"
$child=(Get-ChildItem -Path $reg).name
foreach($item in $child)
{
    Remove-item -Path registry::$item -Recurse #-ErrorAction Inquire -WhatIf
}

Write-Host "All profiles removed successfully" -ForegroundColor Green
"`n"
Write-Host "Now create new profile for outlook" -ForegroundColor Green
"`n"
New-Item -Name "outlook" -Path $reg -Force -Verbose
Write-Host "New profile created" -ForegroundColor Green
"`n"
Write-Host "Launch outlook with newly created profile" -ForegroundColor Green
Start-Process 'outlook' -ErrorAction SilentlyContinue -ArgumentList '/profile "outlook" '

Solution 1:

The best way to create a mail profile from powershell is probably by importing a PRF file as described in this article: https://www.howto-outlook.com/howto/deployprf.htm#script

I'll make a short set by step guide

Download the OCT files

  • Office 2007
  • Office 2010
  • Office 2013
  • Office 2016

Place the Admin folder which u extract from that install into the directory with the installation of the Office Version and then run from the command line setup.exe /admin

After doing so You will get a office Setup and you can skip everything and go right away to Outlook Profile enter image description here Enter the settings you desire in here

After doing so head over to Export Settings and save the PRF file somewhere on the network.

Now there are 2 ways of doing this

  1. Launching Outlook.exe with a parameter Execute Outlook.exe /importprf "\\path\to\your\prf\file.prf" You should only run this command once though. So as a login script which keep being fired of it could be a bad idea.
  2. Setting a Registry Key to import the file
    • Key: HKEY_CURRENT_USER\Software\Microsoft\Office\<version>\Outlook\Setup
    • Value name: ImportPRF
    • Value type: REG_SZ
    • Value: path to prf-file

For this Registry value to work, the FirstRun and First-Run value may not exist in the Setup key.

This way it will only import the file once when outlook is first started.