Posted On January 16, 2020

PowerShell: Purge User Outlook Profile

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Purge User Outlook Profile
# Purge-User-Outlook-Profile.ps1

# Set folder path
$folderToDelete="$env:localappdata\Microsoft\Outlook";

function purgeFolder($path){
mkdir c:\temp -force -ea SilentlyContinue | out-null
cd c:\temp
$childItems=get-childitem $path -Recurse -force;
$filesToDelete=($childItems| ?{ !$_.PSIsContainer }).FullName;
$subFoldersToDelete=($childItems| ?{ $_.PSIsContainer }).FullName;

if (!(get-command handle.exe)){
if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {
Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))};
choco install handle -y;
$handles = handle;
}else{
$handles = handle;
}

function findPidLockedFile{
param($lockedFile)
$scrollingPid="";
$lockingPids=@();
foreach ($line in $handles) {
if ($line -like "*pid:*") {$scrollingPid=$line}
if ($line -like "*$lockedFile*") {
#[void]($scrollingPid -match "(.*)\spid:");
[void]($scrollingPid -match "pid:\s(.*)\s");
$lockingPids+=,$matches[1];}
}
$lockingPids=$lockingPids|get-unique
Write-Warning "$lockedFile is being locked by pid(s): $lockingPids"
return $lockingPids;
}

# Purge Files
foreach ($x in $filesToDelete){
try{
Remove-Item $x -Force
}
catch{
$lockingPids=findPidLockedFile $x;
$lockingPids|%{stop-process -id $_ -force};
Remove-Item $x -Force
}
}

# Then Purge Folders
if ($subFoldersToDelete){$subFoldersToDelete | Remove-Item -force -Recurse}
}

purgeFolder $folderToDelete

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Get Spectre Meltdown Patching Versions of Hyper-V Hosts

function getHyperVHostsInForest{ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 = "https://download.microsoft.com/download/1/D/8/1D8B5022-5477-4B9A-8104-6A71FF9D98AB/WindowsTH-RSAT_WS_1709-x64.msu" $rsat1803…

PowerShell: Windows Servers Systems Inventory

Version 0.03 <# Systems-Inventory.ps1Version: 0.03 -- deprecated 12/24/2019Purpose: to generate a CSV spreadsheet with information…

PowerShell: How to Create Multiple Arrays or Columns from an Array

# This snippets scans the localhost for common ports and outputs 4 arrays $limitPorts=10000 $netstat=netstat…