YEMyslf
YEMyslf

Reputation: 447

Powershell script to query specifc OU in AD for computer names and export

I'm pretty much(read completely) a novice with PowerShell and I'm working on a project to inventory installed software on some computers on our network. I dug up a script to handle the software inventory part, but it relies on a text file with computer names to scan. I'm looking for a way to query AD for computer names for specific OU's. Ideally the script would prompt the user to enter the OU and path of text file to export to. Then it would search AD for a match and if found export to the specified path. So for example, to export all computers in OU=Workstations,OU=New York,DC=Fabrikam,DC=com" the user should receive a prompt and enter "New York" and all computer names would be exported to the specfied path. Any help would be greatly appreciated.

Upvotes: 3

Views: 31753

Answers (2)

gsky
gsky

Reputation: 111

   $City = Read-Host 'City'
   $file = Read-host 'File path'
   try{
   $ou = "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"
   Get-ADComputer -Filter '*' -SearchBase $ou -properties DNSHostName | %  
   DNSHostName | Out-File -Filepath $file
   }
   catch {
   $error[0].exception.message
   }

this will be a bit faster and less memory consuming.

Upvotes: 2

HAL9256
HAL9256

Reputation: 13493

To do it simply, use the Active Directory module in PowerShell.

You can retrieve the computer names with a script similar to this:

$City = Read-Host 'City'
$ou = "OU=Workstations,OU=$City,DC=Fabrikam,DC=com"

$Computers = Get-ADComputer -Filter '*' -SearchBase $ou

$Computers | Foreach { $_.DNSHostName } | Out-File -Filepath "output.txt"

Upvotes: 6

Related Questions