Link to home
Create AccountLog in
Avatar of AXISHK

asked on

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]}
Avatar of Qlemo
Flag of Germany image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.
Avatar of AXISHK


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
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 :-)

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.
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?

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).
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]}
Domain Admins
Domain 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.