1

I am querying list of domain joined computers using powershell as below:

dsquery.exe computer > "C:\testfolder\host.txt"

Output is working as expected, but it is as shown below:

"CN=WIN-20CCF3DC8D,OU=Domain Controllers,DC=hosting,DC=xyz,DC=com"
"CN=WIN-20XYS8CM7D,OU=Computers,DC=hosting,DC=xyz,DC=com"

Here I need the following to do.

Either:

I need to edit this file (using powershell): remove all " character, remove all entries where OU not equal to Computers.

Or

I need to get contents of CN to a string but without ".

Have tried using

$contents = Get-Content C:\testfolder\host.txt | Foreach-Object {$_ -replace '"', ""}

but not seems to be working. Can someone please help me on this.?

serverstackqns
  • 764
  • 3
  • 16
  • 42

1 Answers1

1

This should work :

$contents = Get-Content C:\testfolder\host.txt | where { $_ -match "OU=Computers" } | Foreach-Object {$_ -replace '"', ""}

Then echo $contents gives :

CN=WIN-20XYS8CM7D,OU=Computers,DC=hosting,DC=xyz,DC=com

This returns only lines containing OU=Computers and removes double quotes from those lines.

krisFR
  • 13,280
  • 4
  • 36
  • 42
  • or you can use this too... (for removing ") Get-Content C:\testfolder\temp_host.txt | Foreach-Object {$_ -replace '"', ''} | sc C:\testfolder\host.txt – serverstackqns Jun 01 '15 at 12:00