0

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.

  • 2
    The simplest solution for deleting a file in all users profile would be something like this: `Remove-Item -Path 'C:\Users\*\AppData\Roaming\Microsoft\Outlook\Outlprnt.file'`. This should be all you need when Outlook generates it new anyway. ;-) – Olaf Jan 21 '21 at 22:46
  • Correct^ Should also apply for the `Rename-Item` portion. – Abraham Zinala Jan 21 '21 at 22:50
  • The [linked post](https://stackoverflow.com/a/50215337/45375) provides details, but in short: inside `"..."`, if you want to reference an expression that goes beyond a simple variable reference (e.g., `$var`), you need to enclose it in `$(...)` (e.g., `$($var[0])`) – mklement0 Jan 21 '21 at 23:21

1 Answers1

0

The simplest way to do what you were asking for, the answer was posted by @Olaf in a comment. If youre looking for a more efficient way to iterate through an array, you can use ForEach.

$alluserprofiles = Get-ChildItem -Path C:\Users\ -name

   Foreach($user in $alluserprofiles){
    
Write-Output "Removing old print file backup for user $user..."
Remove-Item -Path "C:\Users\$user\Appdata\Roaming\Microsoft\Outlook\backup\outlprnt.old" -ErrorAction SilentlyContinue

Write-Output "Backing up current print file for user $user..."
Rename-Item -Path "C:\Users\$user\Appdata\Roaming\Microsoft\Outlook\outlprnt" -NewName "outlprnt.old"
    
Write-Output "Completed successfully for user $user."
   
Write-Output "Starting $user in 5 seconds..."
Start-Sleep -s 5
}
Abraham Zinala
  • 4,267
  • 3
  • 9
  • 24
  • 1
    Suggesting a `foreach` statement to loop over an in-memory array is a good tip, but please use proper indentation in your code. – mklement0 Jan 21 '21 at 23:33