Ceej
Ceej

Reputation: 11

Need assistance with PowerShell Script to clean up computers in AD that meet criteria

Hello & thanks in advance for the help! Looking to delete computers (Workstations OU) in AD if they meet a certain criteria.

I need to make sure they have the "LOCATION," part of the Canonical name in common before proceeding to delete. If they are not at my location that could be reason to investigate and I do not want to delete them. This is an example of one PC (Caps are fields I changed):

ORGANIZATION.COM/Workstations/BUSINESS UNIT/Desktops/LOCATION/COMPUTER NAME

I have the following script currently that will print them to a .csv which is helpful, but to take it one step further, it would be nice to print this on the screen then review it quick and proceed with a delete. Any tips??

Get-Content C:\Temp\Powershell\hosts.txt | ForEach-Object {
  Get-ADComputer $_ -Properties Name,CanonicalName |Select-Object Name,CanonicalName
} -ErrorAction Ignore | Export-Csv C:\Temp\Powershell\Output.csv

Or even a second line of code I can utilize the output.csv with, not sure where to go from here...

Again, Thanks!

Upvotes: 1

Views: 290

Answers (1)

Ceej
Ceej

Reputation: 11

Added -Recursive and it seems to be working as expected.

Upvotes: 0

Related Questions