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: Add Accounts into Local Administrators Group

# addAccountToLocalAdmins.ps1# Requires: PowerShell Version 4.0+$accountToAdd='ServerAdmins'$groupName="Administrators"$servers=@( "CONCU1", 'CONCU2', 'CONCU100', 'CONCU80665', 'CONCU6547354', 'CONWHAT989', 'CONCU3')$servers|%{ $session=new-pssession $_…

PowerShell: Add User To Group in Active Directory

$userId='kimconnect' $groupName='Remote Desktop Users' function addUserToGroup($userId,$groupName){ $userExists=Get-ADGroupMember $groupName|?{$_.SamAccountName -eq $userId} if(!$userExists){ Add-ADGroupMember -Identity $groupName -Members…

PowerShell: Move Files Into Zip

# moveFilesToZip.ps1 # Version 0.02 # This little function is to automate the process of…