Find Folder or File and then Get-ACL with information

Northway 21 Reputation points

I have a script I am working on to get user input for the following things.
Workstation Name
User Name
Software Name
Then using that information perform a Get-childitem to see if there are any folders in the C drive or user with the name of the software
If there is then perform a get-acl with the information.
Where I am having trouble so far is using the information received from get-childitem.

$GetassetName = Read-Host "Enter the Workstation Name"  
$assetName = $GetassetName  
$GetuserName = Read-Host "Enter the Users Account Name"  
$userName = $GetuserName  
$GetSoftwareName = Read-Host "Enter the Software Folder Name)"  
$SoftwareName = $GetSoftwareName  
$GetChildItemsUserFolderDirectory = Get-ChildItem -Path \\$assetName\C$\Users\$userName\ -Filter $SoftwareName* -Recurse | Format-Table -Property Directory  
$GetChildItemsCDriveDirectory = Get-ChildItem -Path \\$assetName\C$\ -Filter $SoftwareName* -Recurse | Format-Table -Property Directory  
$GetChildItemsUserDirectoryName = Get-ChildItem -Path \\$assetName\C$\Users\$userName\ -Filter $SoftwareName* -Recurse | Format-Table -Property Name  
$GetChildItemsCDriveDirectoryName = Get-ChildItem -Path \\$assetName\C$\ -Filter $SoftwareName* -Recurse | Format-Table -Property Name  
$GetACLCDrive = get-acl -Path $GetChildItemsCDriveDirectory\$GetChildItemsCDriveDirectoryName -filter $SoftwareName* | format-list -Property PSPath, Sddl, owner  
$GetACLUser = get-acl -Path $GetChildItemsUserFolderDirectory\$GetChildItemsUserDirectoryName -Filter $SoftwareName* | format-list -Property PSPath, Sddl, owner  
try {  
    if ($GetACLCDrive) {  
    if ($GetACLUser) {  
catch {  
    Write-host "Nothing was found"  
Windows Server Security
Windows Server Security
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Security: The precautions taken to guard against crime, attack, sabotage, espionage, or another threat.
1,681 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,273 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 30,826 Reputation points

    I am still learning PowerShell
    see who the owner of it is.

    Ok. Start by reviewing this document. It's been around for a while but it's a very good reference.

    What are you writing the script with? Notepad? You should be using Powershell_ise or VSCode so that you can take advantage of the IDE's autocomplete functionality.

    A common mistake that I see new users make is that they write (or copy) some code and pipe output to multiple cmdlet's and when they don't get the results that they expect they ask us "well why didn't that work".

    $variable = cmdlet | cmdlet | cmdlet   

    Think of writing a script like you are building a brick house. You get one brick in place (a working PS statement) and move on to the next brick.

    Be verbose and display variable contents to verify that you are processing the data that you think you are.

    Test portions of the code (bricks) one at time and get them working before moving on to the next section of code. Hard code key variable so that can easily retest as you make changes. You can clean up and remove excess displays after you get everything working.

    $assetName = 'localhost'  
    $username = 'madne'   
    $SoftwareName = 'onedrive'   
    $target =  "\\$assetName\C$\Users\$userName\"  
    "Processing {0}" -f $target  
    $files = Get-ChildItem -Path $target  -Recurse  
    "I found a total of {0} files and folders" -f $files.count   
    $files = Get-ChildItem -Path $target -Filter $SoftwareName* -Recurse   
    "Adding a {0} filter found  {1} files and folders" -f $SoftwareName, $files.count   
    if ($files.count -eq 0) {  
        "Unable to continue, nothing to process."  
    $FirstName =  $files[0].FullName  
    "The first item that we found is {0}" -f $FirstName  
    $acl = Get-Acl -Path $FirstName   
    "Here is the acl."  
    $acl |Format-List -Property *  
    "Here is the owner {0}" -f $acl.Owner  

2 additional answers

Sort by: Most helpful
  1. MotoX80 30,826 Reputation points

    When writing code, display the contents of the variables or results as part of the script so that you can see what data you are working with.

    "----- Here is what Get-childitem sees ---------"   
    Get-ChildItem -Path \\$assetName\C$\Users\$userName\ -Filter $SoftwareName* -Recurse   
    "----- Here are those results formatted into a table  ---------"   
    Get-ChildItem -Path \\$assetName\C$\Users\$userName\ -Filter $SoftwareName* -Recurse | Format-Table -Property Directory  
    "----- Here is GetChildItemsUserFolderDirectory  ---------"   
    $GetChildItemsUserFolderDirectory = Get-ChildItem -Path \\$assetName\C$\Users\$userName\ -Filter $SoftwareName* -Recurse | Format-Table -Property Directory  

    You are trying to use variables like $GetChildItemsCDriveDirectory as a string that contains a directory name. But Get-Childitem will likely return multiple objects. And I don't see what you are trying to accomplish by formatting it into a table with only the Directory property, which won't work anyway because there is no directory property.

    You can display all of the properties of the first item found like this. You probably want Fullname.

    (Get-ChildItem -Path \\$assetName\C$\Users\$userName\ -Filter $SoftwareName* -Recurse )[0] | format-list -property *   

  2. Mr Galal Azam 1 Reputation point

    Thank you

    0 comments No comments