0

I want to configure the Outlook profile with the email account and I'm taking the values from the stored credentials. But when I run the script, it shows

Start-Process : This command cannot be run due to the error: The filename or extension is too long.

The code is as follows:

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\16.0\Outlook\Profiles"

Write-Host "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

$Login = Get-StoredCredential -Target 'test'
$Decrypt = $Login.Password | ConvertFrom-SecureString
$Encrypt = $Decrypt | ConvertTo-SecureString  -AsPlainText -Force
$passwd=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Encrypt))
$Password = ConvertTo-SecureString $passwd -AsPlainText -Force 
$cred = new-object -Typename System.Management.Automation.pscredential -ArgumentList($Login.UserName, $password)
Start-Sleep -s 5
Start-Process  'OUTLOOK.EXE' -Credential $cred -ArgumentList '/profile "outlook" ' 

Error:

Start-Process : This command cannot be run due to the error: The filename or extension is too long.
At line:1 char:2
+  Start-Process  'OUTLOOK.EXE' -Credential $cred
+  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessComm 
   and

In this script, I'm able to create a new profile but unable to launch the outlook with newly created profile including the email account part.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220

1 Answers1

0

The Start-process raises an error when providing Credentials - possible bug page has the following explanation to the problem:

When you use Start-Process without specifying a target directory with -WorkingDirectory, PowerShell's current location (directory) is used for the target process as well. Since you're using -Credential to run as a different user - without elevation at that point - the target user may lack permission to access the current directory, which happens if the current directory is inside the current user's home directory subtree, for instance. Unfortunately, PowerShell's error message obscures this cause by misleadingly reporting: The directory name is invalid.

The solution is to make sure that the current location is accessible to the target user, or, preferably, use the -WorkingDirectory parameter to explicitly set the target process's current directory.

For example, to start the target process from the directory in which a target script is located, you could use something like:

$script = 'c:\path\to\your\script.ps1'
Start-Process -WorkingDirectory (Split-Path $script) -Credential ...
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • [about_Automatic_Variables $MyInvocation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.1#myinvocation) - you could use `$PSScriptRoot` if it's actually part of a script. `Get-Location` would also get you the present working directory instead of hardcoding that path. – Seth Aug 16 '21 at 05:57
  • 1
    Hi,thanks for your response.I was keep on getting the above mentioned error.I tried included the Start-Process 'outlook' -ArgumentList '/profile "outlook" ' -WorkingDirectory (Split-Path $script) -Credential $cred but is till getting the same error.could you please help on this.I was trying to open the outlook by providing credentials which the email account also should configure in it. – chaithnaya Aug 16 '21 at 06:00