Get only computer names from AD

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 02:48:21

问题


I'm new to Power Shell and I'm testing some commands and ideas. I'm stuck on what I feel should be pretty simple. I want to pull the names of computer objects in AD in to a file. The method I'm trying so far is this

$computers = Get-ADComputer -Filter * | Format-List name
write($computers) | Out-File -FilePath .\computers.txt

the issue I have with this though is that the file that is output looks like this

name : SERVER1

name : SERVER2

name : WORKSTATION1

name : WORKSTATION2

And I'm looking for just a straight list of names with out the "name : " part that comes in front. I'm not sure if there is a better way to accomplish this.


回答1:


This should be faster than @MDMoore313's solution by virtue of not looping through the results & writing to disk on each trip through (1.7s for his vs. 1.1s for mine in my AD environment, writing to a RAMDisk):

$computers = Get-ADComputer -Filter * | select-object -expandproperty name | out-file .\computers.txt



回答2:


If you are using PowerShell 3, you could use short-hand dot notation below.

(Get-ADComnputer -filter *).name > .\computers.txt
or
(Get-ADComnputer -filter *).name | out-file .\computers.txt



回答3:


Here's the plain bread & butter:

Get-ADComputer -Filter * | Format-Table name

Just change the word "List" in your command to the word "Table" and you get exactly what you asked for - no "name:" text in front of the hostnames (and no line-feed between each result, either!).




回答4:


$computers = Get-ADComputer -Filter * | foreach { $_.Name | Add-Content -Path .\computers.txt}



回答5:


I found (get-adcomputer -filter *).name > .\computers.tx not working but it works fine if there is some modification for the original positing:

$computers = Get-ADComputer -Filter * | Format-table name

write($computers) | Out-File -FilePath .\computers.txt


来源:https://stackoverflow.com/questions/19164485/get-only-computer-names-from-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!