Funkel
Funkel

Reputation: 183

Need assistance with confusing powershell issue

I wrote a powershell script for use at my company. For whatever reason the part of the script that is supposed to remove all .ost files at the location specified only works some times. the Path for the ost does not change. Anyone have any idea's why this would be?

Stop-process -Name OUTLOOK -ErrorAction SilentlyContinue -Force
Stop-process -Name communicator -ErrorAction SilentlyContinue -Force
Stop-process -Name lync -ErrorAction SilentlyContinue -Force
Stop-Process -Name UcMapi -ErrorAction SilentlyContinue -Force
Stop-Process -Name skypehost -ErrorAction SilentlyContinue -Force
Stop-Process -Name searchprotocalhost -ErrorAction SilentlyContinue -Force

$OstPath = "c:\users\$([environment]::username)"+ "\AppData" + "\local" + "\Microsoft" + "\Outlook" 
$ost = get-ChildItem $OstPath | where { $_.Extension -eq ".ost"}  
$ost | remove-Item -WhatIf

Start-Process Outlook

Upvotes: 0

Views: 113

Answers (1)

Tyler Helder
Tyler Helder

Reputation: 624

you will want to change you code to something similar to the below. I tested the below in my test environment and it worked without issues.

$OSTPath = Get-Item -Path "$env:USERPROFILE\AppData\Local\Microsoft\Outlook\*" -Filter '*.ost'

If ($OSTPath)

    {
        Write-Output "OST File Found, Deleting File...."
            Remove-Item -Path $OSTPath -Force
    }

ELSE

    {
        Write-Output "No OST Exists. No Action Taken."
    }

Since solution wasn't posted, thought I would add a solution for anyone that might come across this question.

Upvotes: 2

Related Questions