learning Powershell and writing this script for practice. The ultimate goal of the script is to delete the "Outlprnt.file" file for every user on a local computer in their %appdata%\Roaming\Microsoft\Outlook directory. That file frequently becomes corrupted and needs to be renamed so Outlook can generate a new one. The code I'm using to do this is below.
$alluserprofiles = Get-ChildItem -Path C:\Users\ -name
[int[]]$userprofiletotal = $alluserprofiles.Count
while ($userprofiletotal -ge 0){
Write-Output "Removing old print file backup for user $alluserprofiles[$userprofiletotal]..."
Remove-Item -Path "C:\Users\$alluserprofiles[$userprofiletotal]\Appdata\Roaming\Microsoft\Outlook\backup\outlprnt.old" -ErrorAction SilentlyContinue
Write-Output "Backing up current print file for user $alluserprofiles[$userprofiletotal]..."
Rename-Item -Path "C:\Users\$alluserprofiles[$userprofiletotal]\Appdata\Roaming\Microsoft\Outlook\outlprnt" -NewName "outlprnt.old"
Write-Output "Completed successfully for user $alluserprofiles[$userprofiletotal]."
$userprofiletotal = $userprofiletotal - 1
Write-Output "Starting $alluserprofiles[$userprofiletotal] in 5 seconds..."
Start-Sleep -s 5
}
This is the part I'm having trouble with:
$alluserprofiles = Get-ChildItem -Path C:\Users\ -name
[int32[]]$userprofiletotal = $alluserprofiles.Count
As this is the output:
$alluserprofiles[Public Default (my user) 3]
The goal is to:
1: Store the names of folders in C:\Users in an array ($alluserprofiles)
2: Create a variable of the total number of values in the array ($userprofiletotal)
3: Use the variable containing the total number of values in the array to get the element in that position of the index ($alluserprofiles[userprofiletotal]
4: Place that element into the path and execute the functions to remove the file (C:\Users(user folder name)\etc.)
5: Subtract 1 from the variable $userprofiletotal to move to the next value in the index
6: Loop until this process has run for all values in the index.
How can I accomplish that with the current method I'm using? If there's a more efficient way to do this (which I'm sure there is), what would that be? Thanks in advance.