Test-Path folders and remove

Ask your 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.
This topic is 4 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

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.             test-path $folder
  4.             if ($true){
  5.             $NewShares+= $folder;
  6.             }
  7.             }
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
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

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: 397
Last visit: Thu Oct 27, 2022 5:57 am

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. foreach ($folder in $myFolderlist)
  3. {test-path $folder}
  4. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 397
Last visit: Thu Oct 27, 2022 5:57 am

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 397
Last visit: Thu Oct 27, 2022 5:57 am

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
This topic is 4 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked