0

Good morning. What I am trying to accomplish is easier said than done. With the code below, I return the following:

Group is owned by: @{samaccountname=aduser1}

Group is owned by: @{samaccountname=aduser2}

Here's the code:

$GroupList = get-content "Z:\text.txt"

ForEach($Entry in $GroupList){

$SubGroups = @()

$AllMembers = @()

$strGroupOwner = Get-ADGroup -identity $Entry -Properties ManagedBy | select managedby 

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select samaccountname

 "Group is owned by: " + $strOwnerName

I simply need to remove '@{samaccountname=' and the '}' at the end from my string $strOwnerName before passing it through to the next step to make my resume something closer to:

Group is owned by: aduser1

Group is owned by: aduser2

All I was able to find on Google was removing 'White Space'. Any help or reading material would be most appreciated.

Thanks!

2 Answers 2

1

In your code $strOwnerName is a PSCustomObject. To convert it to a string change

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select samaccountname

to

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select -ExpandProperty samaccountname

or

$strOwnerName = (get-aduser -identity $strGroupOwner.managedby).samaccountname
Sign up to request clarification or add additional context in comments.

4 Comments

Works great! Although, I would still like to know how to rip apart strings and piece them back together again. Any takers?
a good place to start string manipulation: technet.microsoft.com/en-us/library/ee692804.aspx (i also amended the answer to include another solution)
Awesome. That link answered my questions. Marking as answer as soon as it will let me. Thank you Raf!
String manipulation is a completely different topic. And bear in mind that in your original version, you weren't actually getting a string - you were getting a string representation of an object.
1

Use the -expandproperty parameter for select-object.

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select-object -expandproperty samaccountname

Also note that I've used select-object instead of the alias select; aliases should be avoided in scripts because they make an assumption about the execution environment which may not always be true.

2 Comments

Thank you for the explanation, I had to mark Raf as the answer as he did it first but you clarified so I gave you an upvote. Thanks!!
You can change who is marked as the answer. First is not always most right - it's not a race.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.