Accessing variables outside foreach inside it in powershell -
I have a simple code sample in which I wanted a loop through the directory contents:
Code> $ path = "c: \ user \ bissau \ desktop \ novel" $ content = dir-path $ path | Where are the items under {$ _ mode-match "^ D. *"} Foreach ($ item in $ content) {"Folder $ path \ $ item:" Dior-path "$ path \ $ item" | Name, mode}}The problem is that the script is not able to read the variable $ path inside the fire-loop loop. Can someone give me some ideas? Is this normal behavior?
Your
foreach command should be shown below, note that printing the line You do not haveecho and you;foreach ($ item $ in content) {"folder $ path \ $ items are itemized under:"; The "path" $ path \ $ item | Select the name, modeYou can try that it's called Dior by using
ForEach-Object commandlet-path $ path. Where {$ _ mode mode "^ D. *"} | There are items under the {Write-host "folder $ path \ $ _ for every object:"; Dir-path "$ path \ $ _" | Name, mode}
Comments
Post a Comment