Split item in Powershell

Is it possible to separate the users in the following command line and join them in a single line with "," as separator ?  Thx

(get-acl D:\dept-home).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
AXISHKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Just enclose the whole expression in     ( ... ) -join ','
0
AlanConsultantCommented:
Hi,

The output may include 'blank' entries and duplicates such as {a,b,,c,a,,,,b} depending on the ACL in question.

To remove blanks only (but leave duplicates):

((((Get-ACL D:\Dept-Home).Access |Select -ExpandProperty IdentityReference |% {$_.Value.Split('\')[1]}) -Join ',') -Split "," | Where{$_ -ne ""}) -Join ","

Open in new window


To remove both blanks and duplicates:

((((Get-ACL D:\Dept-Home).Access |Select -ExpandProperty IdentityReference | Select -Uniq | % {$_.Value.Split('\')[1]}) -Join ',') -Split "," | Where{$_ -ne ""}) -Join ","

Open in new window



Alan.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Alan, I might be overlooking something, but I don't know why you use -join ',' -split ',' here. Unless the data itself contains comma-separated values (which it does not, AFAIK), this is redundant, and can be removed.
I cannot find a reason for IdentityReference (or its split) being empty unless the ACL itself is empty, or the split being empty, which means the ACL is not following the namespace\object format?

As I see it, the whole expression, removing dups, can be significantly simplified (changing much of the original command):
((Get-ACL D:\Dept-Home).Access.IdentityReference.Value -replace '.*\' | Sort -Unique | ? { $_ }) -join ','

Open in new window

This assumes IdentityReference is never empty.
1
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

AXISHKAuthor Commented:
The powershell has error...

The regular expression pattern .*\ is not valid.
At line:1 char:1
+ ((Get-ACL C:\).Access.IdentityReference.Value -replace '.*\' | Sort -Unique | ?
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (.*\:String) [], RuntimeException
    + FullyQualifiedErrorId : InvalidRegularExpression
0
AlanConsultantCommented:
I get the same issue on the machine I just tested it on (PSVersion = 2.0)

Edit:  Also on PSVersion 5.1

To answer your more general query about how I constructed the command, I was seeking to take the original command supplied in the OP, and 'fix' it, rather than re-write it, hence I left:

(get-acl D:\dept-home).Access |select -expandproperty identityreference

Open in new window


then converted it to get all on one line, and remove the blanks / duplicates.

I figured the OP could then generalise if they need to do the same to other similar outputs.

I'm sure most of my code could be improved to look better, be shorter, or to run faster - I tend to stop once it works :-)

Alan.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Sorry for  the (copy&paste) error, it should be -replace '.*\\'.

Alan, the main point is that "stop once it works" is no good base for code. Something obvious like a join split combo needs to trigger some alerts.
Blanks or empty values are often an indication of an error made somewhere. As said, it would be important to know why you (think to) see those.
0
AlanConsultantCommented:
Hi Qlemo,

If you run the command as it was posted in the OP and
Just enclose the whole expression in     ( ... ) -join ','
do you ever see any blanks or duplicates?

Alan.
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Dups yes, but no blanks. The dups are a good point, but already contained in the original question, so removing them is "not necessary" and an improvement (I should have considered myself).
0
AlanConsultantCommented:
Interesting - I see blanks too sometimes.

This is what I get on a 'test' folder I created under T:\Company\

First, using the original command:

PS C:\Temp> (get-acl T:\Company\Test\).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
Example_SecGroup_TSRemoteUsers
BackupOperator
SYSTEM
Domain Admins
Domain Users
Administrators
Users

Open in new window


Which looks to be what you would expect.

Now with your command:

PS C:\Temp> ((get-acl T:\Company\Test\).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}) -join ','
Example_SecGroup_TSRemoteUsers,BackupOperator,,SYSTEM,Domain Admins,Domain Users,Administrators,Users

Open in new window


So with the original command, I don't get a 'blank', but with your command, I get a 'blank' (two commas in a row) between 'BackupOperator' and 'SYSTEM'.

Also interesting is if I right-click on the folder, select properties, and security, I also see 'Creator Owner' listed, which is missing from all of the above.

I do not see any 'blank' line or any other line that is not listed above that would explain the blank in the list from your command.

Alan.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.