How can i get the object information of a list of servers from powershell

Jason Yu
Jason Yu used Ask the Experts™
on
I have a list of over 20 servers need to know their current OU/location and move them to a new OU.

I already used the following command to collect these servers, but i don't know which command I need use to get their current object/OU information, can someone here help?

Thanks.

PS C:\Windows\system32> Get-ADComputer -Filter {Name -like "*host*"} | select Name

Name
----
DAL-OHOST-FS
MIA-OHOST-01
STA-OHOST-01
VCR-OHOST-01
BPDR-OHOST-01
MTY-OHOST-01
NVA-OHOST-01
PRI-OHOST-01
PRI-1D-SCHOSTAG
DUR-OHOST-01
HOU-OHOST-FS
SFO-OHOST-01
ATL-OHOST-01
WAL-OHOST-01
PHI-OHOST-01
BOS-OHOST-01
MTL-OHOST-01
DAL-OHOST-01
CAL-OHOST-01
HOU-OHOST-01
WDC-OHOST-01
MCL-OHOST-01
LOM-OHOST-01
CHI-OHOST-01
MEX-OHOST-01
MIN-OHOST-01
NA-EXCHHOST-01
GLN-OHOST-01
NYC-OHOST-01
LAX-OHOST-01
TOR-OHOST-01
BUR-OHOST-01
TDC-OHOST-01
POR-OHOST-01
DA3-OHOST-01
CBS-OHOST-01
PRI-1B-SCHOSTAG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010
Commented:
DistinguishedName.
Get-ADComputer -Filter {Name -like "*host*"} | select Name, DistinguishedName

Open in new window

That value describes the full path in the directory. You also get the location from canonicalName, but that's no really much good other than looking prettier.
Get-ADComputer -Filter {Name -like "*host*"} -Properties canonicalName | select Name, DistinguishedName, canonicalName

Open in new window

However, you don't actually need any of that when moving things unless there's another reason you want it?

Chris
Top Expert 2014
Commented:
Just by looking at the DistinguishedName property instead of the Name property you can see their OU.  So you could just change the Select-Object command.

To move objects, you can pipe directly from the Get-ADComputer cmdlet to Move-ADObject.
Get-ADComputer -Filter {Name -like "*host*"} | Move-ADObject -TargetPath "OU=someOU,DC=domain,DC=com"

Open in new window

Author

Commented:
could you give me an example of using Move-ADObject for my case?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Chris DentPowerShell Developer
Top Expert 2010

Commented:
footech has provided an example of the move operation already.

Chris

Author

Commented:
Thanks, I appreciate your help.

Author

Commented:
Hi, Chris and Footech, thank you two very much, I learned it.

BTW, if I want to learn powershell from the most basic step, where should I begin?
Chris DentPowerShell Developer
Top Expert 2010

Commented:
I've heard reasonably good things about this one, a few people I know used it to get started:

http://www.amazon.co.uk/Learn-Windows-PowerShell-Month-Lunches/dp/1617290211

There's a lot to be said for experimenting as well (I come from that school, never read any of the books). For the things you might do manually think how you might do them in PowerShell instead. AD is a good technology to work with for this, there's lots you can do (especially searching and reporting) by extending on the commands above.

Inside PowerShell there's help available too, for example, if you wanted to know what else was available in the Active Directory module you might run:
Get-Command -Module ActiveDirectory

Open in new window

Of course, don't forget Get-Help (or Get-Help -Examples if you want to skip right to that). Commands tend to be pretty predictably named, plenty of room for exploration.

Chris
Top Expert 2014

Commented:
I've never read any books either (though I've heard the one above mentioned plenty).

If you get into the habit of reading the built-in help, I think it will help you down the road for all the times when you can't remember exactly how the parameter works or what it's name is.   Even reading the help for cmdlets you're familiar with can reveal different ways of using it, or parameters you had forgotten about.

I like the "Hey, Scripting Guy!" blog posts.

I think the best thing for learning is to pick issues that you're motivated to solve.  Sometimes they could be issues you're facing, other times things you think you should know.  I know I learned a lot by solving/investigating users' questions on Experts Exchange.  I was more motivated to help them solve a problem than just gathering theoretical knowledge.

Author

Commented:
thanks, I will read this book for begining.

have a nice day!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial