Author Posts

October 17, 2013 at 11:25 am

hello I am a newbie with power shell and am looking for guidance on remoting. I am looking to delete a folder that exist on a few machines on my network. This is the command im looking to execute "icm -ComputerName (Get-ADComputer -Filter * | where name -Like a* | select name | ft -HideTableHeaders) {Remove-Item C:\Windows\Sun\Java}"

basically all the workstations that have a java issue begin with the letter "A" and I want to delete a folder that every machine has in the directory. I was wondering if I was headed in the right direction or is there an easier way ? The get-ADComputer cmdlet does return everything I want so as far as the results go I am satisfied.

Thanks for your time and knowledge

October 17, 2013 at 11:40 am

As an initial suggestion, consider using the -Filter parameter of Get-ADComputer. Right now you're getting ALL domain computers... except that the domain will only return about 1,000 max. Something like "Get-ADComputer -Filter { Name -like a* }" might be what you're after, and then you can eliminate that hideously-inefficient Where-Object call :).

You're vaguely headed in the right direction. But you're formatting the information, which ruins it for any other use. The output of a Format command is completely unusable as data. I know you think you're producing a text list of names, but that's not what's happening under the hood (check out "Learn Windows PowerShell 3 in a Month of Lunches;" this business with the Format cmdlets is a major gotcha for newcomers, and there's a whole chapter on it).

(Get-ADComputer -Filter { Name -like a* } | Select -Expand Name)

Is closer to what you want. The -ComputerName parameter of Invoke-Command wants a list of computer names, not an object property. The -ExpandProperty parameter of Select-Object will "extract" the names from the Name property, giving you what you want. Actually, this exact trick is in one of the chapters of that "Month of Lunches" book, also – it goes through several ways of jamming computer names into other commands for various circumstances.

October 17, 2013 at 11:52 am

I do have that book, thanks. its great book and I just watched the MVa on powershell 3.0 which piggybacks that as well. great info now just extracting all that from my brain, lol. i will review that chapter again. thanks

October 17, 2013 at 11:59 am

And, as a quick review for anyone else coming across this...

Get-ADComputer outputs an OBJECT. That's a data structure in memory. Unfortunately, the -ComputerName parameter doesn't want an object... it was STRINGS (computer names). The "Name" property of the ADComputer object contains a string.

However, formatting isn't a valid way to extract strings from properties. Format cmdlets also produce objects... and they're very specialized objects that can really only be consumed by a few "Out-" cmdlets for display on-screen, on paper, etc. Once you format something, you're done – the output of a format cmdlet can't really be used for anything but visual presentation. Hiding the table headers doesn't change that. For example:

Get-Process | Select Name | Format-Table -Hide | Get-Member

Shows that the output isn't just a bunch of strings, although when DISPLAYED that's what the output will look like.