0

I'm trying to knock out a simple enough (I thought) script to clean up older versions of Zoom that were deployed via browser but leaving fully installed versions alone.

Put together the following:

 $UserDir = "C:\Users"
$TargetFolder = "AppData\Roaming\Zoom\bin"

Get-ChildItem $UserDir -Directory -Exclude Default*,Public | foreach {
    
        $joined_path = Join-Path -Path $_,FullName -ChildPath $TargetFolder

        if (Test-Path $joined_path) {
            remove-item "$joined_path\" -Force
            }
        }

And it 'kind of' works, I'm getting the following error:

remove-item : Cannot find path 'C:\Users\Admin\AppData\Roaming\Zoom\bin FullName\AppData\Roaming\Zoom\bin\' 
because it does not exist.

It's apparent that I'm not understanding the join-path command quite right, and it's duplicating the path it's searching for.

The idea here is to delete the target directory (\AppData\Roaming\Zoom\Bin) from all profiles - ironically perhaps the error messages show that it is searching through multiple profiles, but it's tagging on the target folder twice and I'm confused as to why.

Added a line to specify the $joined_path variable as null so as to then add some variable outputs to try and understand where I'm going wrong:

$UserDir = "C:\Users"
$TargetFolder = "\AppData\Roaming\Zoom\bin"
$joined_path = ""

Get-ChildItem $UserDir -Directory -Exclude Default*,Public | foreach {

write-output "resulting joined path1 is" $joined_path
    
        $joined_path = Join-Path -Path $_,FullName -ChildPath $TargetFolder

        write-output "resulting joined path2 is" $joined_path

        if (Test-Path $joined_path) {
            remove-item "$joined_path" -Force
            }
            $joined_path = ""
        }

Now I'm seeing the following in the output:

resulting joined path1 is

resulting joined path2 is
C:\Users\Admin\AppData\Roaming\Zoom\bin
FullName\AppData\Roaming\Zoom\bin

This indicates to me that it's the line between the two outputs, just struggling to understand why.

RobB
  • 1
  • 2
  • 2
    You have a typo: `$_,FullName` should be `$_.FullName`. Also, `-Exclude` only works when the path includes wildcards: `Get-ChildItem $userDir\*` – zett42 May 30 '22 at 12:44
  • Hah! A damned typo. Thanks! I should note though that the -Exclude appears to work just fine for me? The output lines I'd added were only showing the user profiles I'd expect? – RobB May 30 '22 at 13:01
  • 1
    Yes, `Get-ChildItem $UserDir -Directory -Exclude Default*, Public` works, but only because the wildcards do not exclude `$UserDir` itself - if you added `User*` as a wildcard, you'd see the problem. Also in _Windows PowerShell_, if you were to use `-LiteralPath $UserDir` (which, conceptually, is the right thing to do), `-Exclude` would be _ignored_ altogether. See [this answer](https://stackoverflow.com/a/38308796/45375) for the pitfalls of `-Include` and `-Exclude`. – mklement0 May 30 '22 at 13:26

1 Answers1

0

Thanks to Zett42.

Fixing the typo got it all working fine, and it does appear to work it's way around all the profile folders except for what I'm excluding - have tested that on multiple devices now and without any problems or missed profiles.

$UserDir = "C:\Users"
$TargetFolder = "AppData\Roaming\Zoom\bin"
$joined_path = ""

Get-ChildItem $UserDir -Directory -Exclude Default*,Public | foreach {
    
        $joined_path = Join-Path -Path $_.FullName -ChildPath $TargetFolder

        if (Test-Path $joined_path) {
            remove-item "$joined_path\" -Force
            }
        }
RobB
  • 1
  • 2
  • 1
    While this code may work for the given `$UserDir`, _in my opinion_ it's best to avoid using `-Include` and `-Exclude` alltogether and use reliable alternatives such as `Where-Object` filtering. See [possible pitfalls](https://stackoverflow.com/a/38308796/45375). Otherwise it will beat you (and anyone else copy-pasting this code) when least expected, e. g. when you copy it as a template into a new project and it suddenly stops working for no apparent reason. – zett42 May 30 '22 at 14:00
  • I'll bear that in mind if I ever need to make another fix like this - usually I'd lean on one of the dev team to make this tbh, programming/scripting isn't really my bag at all. – RobB May 31 '22 at 15:03