Link to home
Start Free TrialLog in
Avatar of mystikal1000
mystikal1000

asked on

Powershell script that will read all of the Outlook profiles and run a command on each Mail profile.

I need a powershell script that will do the following.

1.  I have a powershell command that will read the subkeys of a mail profile
Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles

Open in new window


The output of the subkeys are below...

Name                                Property
----                                      --------
cool Outlook Profile
Outlook Profile
outlook



2.  I need each of the subkeys into a variable, how do I do that in Powershell?

3.  After I have that answer, I would like to run the command for each subkey variable, inst.exe /u /service dmol /p "Variable=mailprofile"

I really appreciate the feedback!
Avatar of Jeremy Weisinger
Jeremy Weisinger

Here's an example. It's gets the profile names from the registry key you specified, loops through those names and runs the command:
#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}

#loop through profile names
foreach ($profilename in $ProfileNames) {

    $command = "inst.exe /u /service dmol /p `"$($profilename)=mailprofile`""
    Write-Host $command
    #Invoke-Expression $command
    }

Open in new window


You'll notice that I have Write-Host $command and #Invoke-Expression $command in the script. Write-Host will just echo out what command it will run. If that looks correct when you run the script. You can then remove the hash (#) from in front of Invoke-Expression (i.e. Invoke-Expression $command) and it will execute the command.

Let me know if that works for you.
Avatar of mystikal1000

ASKER

Thanks Jeremy, it looks good when it writes the host command, but fails when I invoke it.  See error below.

I accidentally left out the "program files (x86)" path, also I need one for 32-bit, which is just "program files".



#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}

#loop through profile names
foreach ($profilename in $ProfileNames) {

    $command = "C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe /u /service dmol /p `"$($profilename)`""
    #Write-Host $command
    Invoke-Expression $command
    }

Open in new window


Error:

x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Ops hxrext\op extensions\inst. ...
+                   ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Ops hxrext\op extensions\inst. ...
+                   ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
x86 : The term 'x86' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At line:1 char:19
+ C:\Program Files (x86)\Ops hxrext\op extensions\inst. ...
+                   ~~~
    + CategoryInfo          : ObjectNotFound: (x86:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
If the path has spaces, it needs to be surrounded by quotes. e.g.:

#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}

#loop through profile names
foreach ($profilename in $ProfileNames) {

    $command = "`"C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe`" /u /service dmol /p `"$($profilename)`""
    Write-Host $command
    #Invoke-Expression $command
    }

Open in new window

Note the use of the backtick (`) in the script. It forces Powershell to interpret the double quote literally instead of using it as a string delimiter. For more info see this link: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6
Updated it, now it gives me this error.

Invoke-Expression : At line:1 char:88
+ ... instl.exe" /u /service dmol /p "cool"
+                    ~
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:88
+ ... instl.exe" /u /service dmol /p "cool"
+                    ~
Unexpected token 'u' in expression or statement.
At line:9 char:5
+     Invoke-Expression $command
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand
 
Invoke-Expression : At line:1 char:88
+ ... instl.exe" /u /service dmol /p "Default Outlook Profile"
+                    ~
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:88
+ ... instl.exe" /u /service dmol /p "Default Outlook Profile"
+                    ~
Unexpected token 'u' in expression or statement.
At line:9 char:5
+     Invoke-Expression $command
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand
 
Invoke-Expression : At line:1 char:88
+ ... instl.exe" /u /service dmol /p "outlook"
+                    ~
You must provide a value expression on the right-hand side of the '/' operator.
At line:1 char:88
+ ... instl.exe" /u /service dmol /p "outlook"
+                    ~
Unexpected token 'u' in expression or statement.
At line:9 char:5
+     Invoke-Expression $command
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : ExpectedValueExpression,Microsoft.PowerShell.Commands.InvokeExpressionCommand
When I run it from the command line it works fine...this is the correct syntax

C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe /u /service dmol /p "Outlook"

- Where "Outlook" is the name of the e-mail profile.
ASKER CERTIFIED SOLUTION
Avatar of Jeremy Weisinger
Jeremy Weisinger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Crazy, Powershell ISE x86 crashes when I run that command.
Can you run the 64bit version?
Same issue.
Strange indeed. So I'm not sure why it's crashing. What is this inst.exe program?

You could try something like this so that it will just crash a new process:
#Get the profile names from Registry Key
$ProfileNames = Get-ChildItem -Path HKCU:\Software\Microsoft\Office\15.0\Outlook\Profiles | %{Split-Path $_.Name -Leaf}

#loop through profile names
foreach ($profilename in $ProfileNames) {

    $ExePath = "C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe"
    $ExeArgs = "/u /service dmol /p `"$($profilename)`""
    Start-Process -FilePath $ExePath -ArgumentList $ExeArgs
    }

Open in new window

Change $command back to this:
$command = "`"C:\Program Files (x86)\Ops hxrext\op extensions\inst.exe`" /u /service dmol /p `"$($profilename)`""
And instead of using Invoke-Expression, use
cmd /c $command

Open in new window

Michael, I receive this error message


cmd : 'C:\Program' is not recognized as an internal or external command,
At line:9 char:5
+     cmd /c $command
+     ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
operable program or batch file.
cmd : 'C:\Program' is not recognized as an internal or external command,
At line:9 char:5
+     cmd /c $command
+     ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
operable program or batch file.
Thanks Jeremy, I was able to figure the crash.  It had to do with our antimalware software.  I appreciate your help!
Glad to help. :)