Copy file to all user profiles (excluding Default, Public, All Users, Default User)
function Copy-ToEachAppData {
param (
[string]$SourceFilePath # e.g., "C:\Folder\FileName.txt"
)
#validate source file
if (-not (Test-Path $SourceFilePath)) {
Write-Error "Source file '$SourceFilePath' does not exist."
return
}
#get the list of user profiles
$userProfiles = Get-ChildItem -Path "C:\Users" | Where-Object {
$_.PSIsContainer -and $_.Name -notmatch "^(Default|Public|All Users|Default User)$"
}
#loop through each user profile
foreach ($profile in $userProfiles) {
$appDataPath = Join-Path $profile.FullName "AppData\Roaming"
if (Test-Path -Path $appDataPath) {
Write-Output "Copying file to $appDataPath"
try {
Copy-Item -Path $SourceFilePath -Destination $appDataPath -Force
} catch {
Write-Warning "Failed to copy to $appDataPath: $($_.Exception.Message)"
}
} else {
Write-Warning "AppData path not found for $($profile.Name)"
}
}
}
Detect File in current users AppData
#get the currently logged-in user
$CurrentUser = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName
#get the current user's profile path
$UserProfile = [System.Environment]::ExpandEnvironmentVariables("%USERPROFILE%")
#define the path to PERSONAL.XLSB
$AppDataPath = Join-Path $UserProfile "AppData\Roaming\Microsoft\Excel\XLSTART\PERSONAL.XLSB"
#check if the file exists
if (Test-Path $AppDataPath) {
Write-Output "Detected"
exit 0
} else {
Write-Output "Not Detected"
exit 1
}
Check for a file in all user profiles (excluding Default, Public, All Users, Default User)
$basePath = "C:\Users"
$relativeUninstaller = "AppData\Local\EXEName.exe"
$found = $false
#loop through all user profiles (excluding system profiles)
Get-ChildItem -Path $basePath -Directory | Where-Object {
$_.Name -notmatch "^(Default|Public|All Users|Default User)$"
} | ForEach-Object {
$userProfile = $_.FullName
$uninstallerPath = Join-Path $userProfile $relativeUninstaller
if (Test-Path $uninstallerPath) {
Write-Host "Found uninstaller at: $uninstallerPath"
$found = $true
}
}
#detection result
if ($found) {
Write-Output "Uninstaller found in at least one user profile."
exit 0
} else {
Write-Output "Uninstaller not found in any user profile."
exit 1
}
Uninstall App in AppData (excluding Default, Public, All Users, Default User) – PSADT
try {
Write-Log -Message "Starting Application XYZ uninstallation tasks..." -Severity 1
$basePath = "C:\Users"
$relativeUninstaller = "AppData\Local\Uninstall.exe"
$AppFolder = "AppData\Local\Application XYZ"
Get-ChildItem -Path $basePath -Directory | Where-Object {
$_.Name -notmatch "^(Default|Public|All Users|Default User)$"
} | ForEach-Object {
$userProfile = $_.FullName
$userName = $_.Name
$uninstallerPath = Join-Path $userProfile $relativeUninstaller
$AppPath = Join-Path $userProfile $AppFolder
if (Test-Path $uninstallerPath) {
Write-Log -Message "Starting uninstallation of Application XYZ for user '$userName'" -Severity 1
Start-Process -FilePath $uninstallerPath -ArgumentList "/S" -WindowStyle Hidden -Wait
Write-Log -Message "Uninstall complete for '$userName'." -Severity 1
} else {
Write-Log -Message "Uninstaller not found for '$userName'; skipping uninstall." -Severity 2
}
if (Test-Path $AppPath) {
Write-Log -Message "Deleting folder: $AppPath" -Severity 1
Remove-Item -Path $AppPath -Recurse -Force -ErrorAction SilentlyContinue
Write-Log -Message "Deleted Application XYZ folder for '$userName'." -Severity 1
} else {
Write-Log -Message "Application XYZ folder not found for '$userName'; skipping deletion." -Severity 2
}
}
Write-Log -Message "Application XYZ uninstallation tasks completed for all user profiles." -Severity 1
} catch {
Write-Log -Message "Application XYZ uninstallation failed. Error: $($_.Exception.Message)" -Severity 3
}
Removing Shortcut on User Desktop (including OneDrive) – PSADT
try {
Write-Log -Message "Searching for 'Shortcut.lnk' in all user desktops (including OneDrive folders)" -Severity 1
$usersPath = "C:\Users"
$linkName = "Shortcut.lnk"
Get-ChildItem -Path $usersPath -Directory -ErrorAction Stop | Where-Object {
$_.Name -notmatch "^(Default|Public|All Users|Default User)$"
} | ForEach-Object {
$userProfile = $_.FullName
$desktopPaths = @()
$regularDesktop = Join-Path $userProfile "Desktop"
if (Test-Path $regularDesktop) {
$desktopPaths += $regularDesktop
}
Get-ChildItem -Path $userProfile -Directory -Filter "OneDrive*" -ErrorAction SilentlyContinue | ForEach-Object {
$oneDriveDesktop = Join-Path $_.FullName "Desktop"
if (Test-Path $oneDriveDesktop) {
$desktopPaths += $oneDriveDesktop
}
}
foreach ($desktopPath in $desktopPaths) {
$lnkPath = Join-Path $desktopPath $linkName
if (Test-Path $lnkPath) {
try {
Remove-Item -Path $lnkPath -Force
Write-Log -Message "Removed shortcut from: $lnkPath" -Severity 1
} catch {
Write-Log -Message "Failed to remove shortcut from: $lnkPath. Error: $($_.Exception.Message)" -Severity 3
}
} else {
Write-Log -Message "Shortcut not found in: $desktopPath" -Severity 2
}
}
}
Write-Log -Message "Shortcut cleanup complete." -Severity 1
} catch {
Write-Log -Message "Failed during user desktop search. Error: $($_.Exception.Message)" -Severity 3
}