0

I really need some help with Powershell, complete novice in Powershell

I have the command below, which outputs a list of paths searching for folder called "xyz" created multiple times on a share, used as a variable

$FOLDERLISTS = (Get-ChildItem \\server\share -Recurse | Where-Object { ( $._PSIsContainer -eq $true) -and ($_.Name -like "xyz" -and ( $_.mode -match "d") | % { Write-Host $_.FullName })

How can I use the multiple folder paths, can I set this as a variable?

Basically I just want to get the folder paths, then run another Get-ChildItem against each folder path the above command outputs, so if it was a single variable the command would looks like;

Get-ChildItem "@ABOVECOMMAND" -Recurse | Where-Object ( !($_.PSIsContainer) -and $_.lenght -le 1000000 )

Can I somehow use ForEach for this to run over the multiple paths?

foreach ($FOLDERLIST in $FOLDERLISTS)
{
Get-ChildItem -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }
}

Or

$FOLDERLISTS | ForEach-Object{
Get-ChildItem -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }

Or just export the paths to a text file and import into the command? Completely stuck.

2
  • Where are you stuck? Don't see why either of your last two suggestions shouldn't work Commented Feb 10, 2015 at 16:59
  • Neither of the last two ideas would ctually work as written. Neither has a variable to reference the current iteration of the loop. In the first idea it would be $FOLDERLIST. In the second idea it would be $_. Commented Feb 10, 2015 at 17:03

1 Answer 1

1

Your first try should be more like this:

foreach ($FOLDERLIST in $FOLDERLISTS)
{
Get-ChildItem $FOLDERLIST -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }
}

OR your second try like this:

$FOLDERLISTS | ForEach-Object{
Get-ChildItem $_ -Recurse | Where-Object { !($_.PSIsContainer) -and $_.lenght -le 1000000 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks campbell I think from your comment I can see where I was going wrong, get-childitem was not passed anything!! I'll give this a try looks like a winner!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.