1

I am fetching AD groups and trying to trim the end space and putting them in a text file. However, this works (Method 1):

$filepath = "A"
$domain = "B"
$ADnames = Get-ADGroup -Server $domain -Filter *| select SAMAccountName | out-file "$filepath\test.txt"
get-content "$filepath\test.txt" | foreach {$_.trimend()} | out-file ""$filepath\test1.txt""

But this doesn't (Method 2):

$filepath = "A"
$domain = "B"
$ADnames = Get-ADGroup -Server $domain -Filter *| select SAMAccountName | foreach-object {$_.trimend()} | out-file "$filepath\test.txt"

Can i not pipeline it directly ?

I do not want to create an additional file like i am doing in method 1.

5
  • 1
    | select -ExpandProperty SAMAccountName | Commented Aug 1, 2017 at 4:30
  • works. Thanks. What was i doing wrong conceptually ? Can you put all this in an answer for future readers please ? Commented Aug 1, 2017 at 4:34
  • This is such a common powershell mistake that I'm sure there are answers with it already explained, although I don't know where specifically because it's not an easy one to search for; I have tried to explain it as part of other answers in the past, but it's mixed in with unrelated things about their question. Conceptually you were keeping the container as well as the contents and you can't trim() the container. Commented Aug 1, 2017 at 4:49
  • The code .. | select-object SAMAccountName outputs roughly this structure: -------------- [psobject]@{ "SAMAccountName" = "Group1" } - a psobject with a name and a value. The code .. | select-object -ExpandProperty SAMAccountName outputs just the content "Group1" as a string Commented Aug 1, 2017 at 4:50
  • @TessellatingHeckler Feel free to copy and paste that first comment into an answer, I'm sure people won't mind it being short because their time is being saved from clicking on an "unanswered" question.. :P Commented Aug 1, 2017 at 4:52

1 Answer 1

2

This Works:

| select -ExpandProperty SAMAccountName |

Explanation:

In my 2 approach, I am trying to trim a container and not its content and hence it was showing error.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.