Test-Path folders and remove

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
localpct
Posts: 298
Joined: Wed Sep 09, 2015 12:53 pm

Test-Path folders and remove

Post by localpct » Thu Jul 25, 2019 11:34 am

We have a collection of folders we get from a script, and the engineers don't like to keep this list up to date. I'm looking for a way to query if the folders exist, and if they do, add them to a new variable

Code: Select all

foreach ($folder in $Shares)
{
test-path $folder
if ($true){
$NewShares+= $folder;
}
}
The problem, my results are

\\share\folder\\share\folder2\\share\folder5

It's not breaking them into individual objects
What I'm looking at getting is

\\share\folder
\\share\folder2
\\share\folder5

User avatar
mxtrinidad
Site Admin
Posts: 354
Joined: Sun Mar 03, 2013 12:42 pm

Re: Test-Path folders and remove

Post by mxtrinidad » Thu Jul 25, 2019 11:45 am

You can create an array variable to store the result from the "foreach" statement.

[array] $myFolderlist = $null;

$myFolderlist = foreach ($folder in $Shares)
{
if ((test-path $folder) -eq $true){
$folder;
}
}

$myFolderlist

Hope this helps!

User avatar
localpct
Posts: 298
Joined: Wed Sep 09, 2015 12:53 pm

Re: Test-Path folders and remove

Post by localpct » Thu Jul 25, 2019 12:28 pm

Yes it does.

Can I use this in a workflow? It would be much quicker to run through my list of 50 folders with a work flow. When I run the below script, it returns nothing

Code: Select all

Workflow TestWorkFlow{
foreach ($folder in $myFolderlist)
{test-path $folder}
}

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Test-Path folders and remove

Post by jvierra » Thu Jul 25, 2019 12:37 pm

This is how to code this kind of coding pattern, PS will accumulate to an array automatically.

Code: Select all

$NewShares = foreach($folder in $Shares){
     if(test-path $folder){$folder}
}
Or even easier:

Code: Select all

$NewShares = $Shares | Where-Object{test-path $_}

A workflow will not give much speed with such short code.

User avatar
localpct
Posts: 298
Joined: Wed Sep 09, 2015 12:53 pm

Re: Test-Path folders and remove

Post by localpct » Fri Jul 26, 2019 7:48 am

Thanks to both of you. I have it working as I need it..

Just too bad workflows won't work :/

User avatar
jvierra
Posts: 13809
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Test-Path folders and remove

Post by jvierra » Fri Jul 26, 2019 8:12 am

Workflows will work. My indication is that they may not be all that much faster although this would depend on your network.

Here is an old script that I found in my junk box.

Code: Select all

workflow Test-WFConnection {
    param(
        [string[]]$Computers
    )

    foreach -parallel ($computer in $computers) {
        Test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue
    }

}

$computers = Get-ADComputer -Filter * | Select -Expand Name
Test-WFConnection $computers

User avatar
localpct
Posts: 298
Joined: Wed Sep 09, 2015 12:53 pm

Re: Test-Path folders and remove

Post by localpct » Fri Jul 26, 2019 12:28 pm

Got it.. It's definitely not a significant increase. Thanks for taking the time to go back to this

Locked