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: 312
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Wed Sep 09, 2015 12:53 pm

Test-Path folders and remove

Post by localpct »

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
  1.     foreach ($folder in $Shares)
  2.  
  3.             {
  4.  
  5.             test-path $folder
  6.  
  7.             if ($true){
  8.  
  9.             $NewShares+= $folder;
  10.  
  11.             }
  12.  
  13.             }
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: 399
Joined: Sun Mar 03, 2013 12:42 pm

Re: Test-Path folders and remove

Post by mxtrinidad »

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: 312
Joined: Wed Sep 09, 2015 12:53 pm

Re: Test-Path folders and remove

Post by localpct »

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
  1. Workflow TestWorkFlow{
  2.  
  3. foreach ($folder in $myFolderlist)
  4.  
  5. {test-path $folder}
  6.  
  7. }

jvierra
Posts: 14355
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Test-Path folders and remove

Post by jvierra »

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: 312
Joined: Wed Sep 09, 2015 12:53 pm

Re: Test-Path folders and remove

Post by localpct »

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

Just too bad workflows won't work :/

jvierra
Posts: 14355
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Test-Path folders and remove

Post by jvierra »

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: 312
Joined: Wed Sep 09, 2015 12:53 pm

Re: Test-Path folders and remove

Post by localpct »

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

Locked