Split item in Powershell

AXISHK
AXISHK used Ask the Experts™
on
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]}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Just enclose the whole expression in     ( ... ) -join ','
Consultant
Commented:
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

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
AlanConsultant

Commented:
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.
AlanConsultant

Commented:
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.
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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).
AlanConsultant

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial