Link to home
Start Free TrialLog in
Avatar of Jay Thomas
Jay ThomasFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Basic Powershell question question. Hopefully

Hi all.
Many a time I run a one liners against multiple servers and so end up running the liner many times. I am looking for a way I can loop in a list of servers from a text file.

My current one liner is this:
Get-ADcomputer -Identify "HostName" -Properties Lastlogon, passwordlastset

Simple! And I can use it but if a have over 100 hostnames in a text file called servers.txt on the C:\temp folder there must be an easy way I can run that command but pipe in that text file?
If I can get the answer to this question it will help not only for my current taks but many times in the future.
Thanks all.
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

You first define the file, then Get-Content to load the items in the file (one per line).

Next, use a foreach loop to iterate through the computers in the list.

$file = C:\Temp\servers.txt
$computers = Get-Content $file

foreach ($computer in $computers)
{
   Get-ADComputer -Identity $computer -Properties lastlogon,passwordlastset
}

Open in new window

Avatar of oBdA
oBdA

In general, you can do that with a ForEach-Object loop, with $_ being the current loop element. So you first read the contents of the file, then do something for each element:
Get-Content C:\Temp\servers.txt | ForEach-Object {Get-ADcomputer -Identity $_ -Properties Lastlogon, passwordlastset}

Open in new window

If you want to save some typing for one-liners, there are two Aliases for ForEach-Object: ForEach and %:
Get-Content C:\Temp\servers.txt | ForEach {Get-ADcomputer -Identity $_ -Properties Lastlogon, passwordlastset}
Get-Content C:\Temp\servers.txt | % {Get-ADcomputer -Identity $_ -Properties Lastlogon, passwordlastset}

Open in new window

This was the ForEach-Object cmdlet. There's a ForEach statement as well:
ForEach ($Line In (Get-Content  C:\Temp\servers.txt)) {Get-ADcomputer -Identity $Line -Properties Lastlogon, passwordlastset}

Open in new window

Avatar of Jay Thomas

ASKER

Brilliant response that you guys. Just going to try each method out now. Back in a bit.
@ Dustin.
I get an error saying 'Cannot bind argument to parameter 'path' it is null.
Ah, try
$file = "C:\Temp\servers.txt"
$computers = Get-Content $file

foreach ($computer in $computers)
{
   Get-ADComputer -Identity $computer -Properties lastlogon,passwordlastset
}

Open in new window

That got it thank you. I hope I am not going off topic but if I add
> c:\temp\svresult.txt
to the end of the Get-ADcomputer code it creates the text file but only has the data from the first server in the server list. Is this to of topic to ask this at this time?
Thank you
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Still another way you can do this is piping directly to the cmdlet.  It depends on the cmdlet what type of input it accepts, but here this would work.
Get-Content "C:\Temp\servers.txt" | Get-ADcomputer -Properties Lastlogon, passwordlastset

Open in new window

Wow. you PS dudes blow me away. That works and so do the one liners oBDa (like they ever weren't going to work :)

I'm giving most the points to Justin, hope that feels fair to all. I would like to say a big thank you chaps. I can see both options here saving me countless hours in the future so thank you very much.
Hats off!