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.
https://www.sapien.com/books_training/Windows-PowerShell-4
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."
return
}
$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