Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

How can I force array context in a Powershell script

$
0
0

Hi,

Create the file c:\temp\temp.txt with the single line "cd foo" (without the quotes).

Then try this:

$dirs=Get-Content c:\temp\temp.txt | Where {$_ -match "^cd "}

$dirs[0]  (equals "c")

Now add the line "cd foo" to the file and repeat the above.

Now $dirs[0]  (equals "cd foo")

It doesn't matter if I initialize $dirs as $dirs=@().

So, how do I force an array context for $dirs, even when Get-Content only returns a single line?  I need $dirs[0] to always return the full line from the file, whether the file contains 1 or >1 matches.

Thanks...



Viewing all articles
Browse latest Browse all 6937

Trending Articles