Extract a portion in powershell result

Run get-acl d:\dept-home  | Select-Object path, accesstostring

Is it possible to simplify the output to show something like  D:\dept-home    Domain Admin...  ?  Thx

Path                                                                                                               AccessToString                                                                  
----                                                                                                                      --------------                                                                  
Microsoft.PowerShell.Core\FileSystem::D:\dept-home                                MYDOMAIN\Domain Admins Allow  FullControl...                            


Thx
AXISHKAsked:
Who is Participating?
 
Daryl BamforthTechnical ExpertCommented:
You already know your $path variable, so just include this at the beginning as a set variable. This means that manipulating the output from get-acl becomes much easier, as we are no longer trying to recreate $path from the powershell path entity.

As you are only concerned about the username portion of the username we can now split the output on '\' and then select the 2nd object in the split array. I do need to point out, however, that this has the potential to throw up duplicates if users have local admin permissions as well as domain permissions you will not be able to differentiate between the two (such as Administrator). For the most part it should be fine though.

(get-acl c:\temp).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}

Open in new window


Full script..

$path = "C:\temp"
$users = @()
$users = (get-acl $path).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
$details = @()
$details = "" |select path, users
$details.path = $path
$details.users = $users

Open in new window

0
 
Pawan KumarDatabase ExpertCommented:
Pls try this -

$path.SubString(($path.LastIndexOf('D:\'),100)
$AccessToString.SubString(($AccessToString.LastIndexOf('Domain Admins'),100)    

Open in new window


you can also replace 100 with $path.length.
0
 
AXISHKAuthor Commented:
For the Access field, I want to format the output like john, Mary, etc which require to extract the value inside the string like mydomain\john .... mydomain\mary .

How can I archive this result ?

Thx
0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
AXISHKAuthor Commented:
The below code works fine but I change one line to remove top folder path, it returns with error :

Cannot find an overload for "SubString" and the argument count: "1".
At C:\Scripts\test.ps1:10 char:5
+     $temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

$temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))




$path = @()
$users = @()
$path = "D:\dept-home"

$details = New-Object System.Collections.ArrayList

get-childitem $path  | foreach {If ($_.psiscontainer)  {
    $temp = "" | select "Folder", "Access" 
    $StrPath = 
    $temp.Folder = $_.Fullname 

    $users = (get-acl $_.FullName).Access |select -expandproperty identityreference |% {$_.value.split('\')[1]}
    $temp.Access = $users
    
    $details.Add($temp) | Out-Null
}}
 write-output $details

Open in new window

0
 
Daryl BamforthTechnical ExpertCommented:
Remove
$StrPath = 

Open in new window

no point in it being there since not assigned to anything.

What is the full scope of what you need to achieve?

If you just want ACLs for a named folder the script I put in does that, to export just add this to the bottom.

$details |export-csv -path c:\temp\ACLexport.csv -notypeinformation

Open in new window


Do you want recursive folder ACLs for all sub folders as well?
0
 
AXISHKAuthor Commented:
Export CSV has been fixed but can't trim the path with the following error :

$temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))

Cannot find an overload for "SubString" and the argument count: "1".
At C:\Scripts\test.ps1:10 char:5
+     $temp.Folder = $_.Fullname.SubString(($_.Fullname.LastIndexOf($path),100))
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
0
 
Daryl BamforthTechnical ExpertCommented:
What are you wanting to trim?

At the moment $_.fullname is returning the full path so

d:\dept-home\folder1

If you just want the 'folder1' bit then change fullname, to name, so $_.name, which would return

folder1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.