Search code examples
powershellvariablesappdata

How to get folder name thats different on all user profiles


I like to know how to get 5J91Q4CX.C10 to use in a variable.

C:\Users\user\AppData\Local\Apps\2.0\5J91Q4CX.C10

On all user profiles this folder has a different name. It is always 8 numbers and digits then a . and then 3 digits or numbers.

I need to use this for a powershell script.

Any idea how I can make a variable for this foldername? Thanks


Solution

  • I'd do something like this:

    #Loop through all user profile folders using something like this:
    $userFolders = Get-ChildItem -Path "C:\Users\" -Directory -Force -ErrorAction SilentlyContinue | 
                    Where-Object { @('All Users','Default User', 'Public', 'Default') -notcontains $_.Name } |
                    Select-Object -ExpandProperty Name
    
    # next loop through these folders to find the foldername that can be different for each user
    foreach ($userName in $userFolders) {
        $folderName = Get-ChildItem -Path "C:\Users\$userName\AppData\Local\Apps\2.0" -Directory -Force -ErrorAction SilentlyContinue | 
                        Where-Object { $_.Name -match '[A-Za-z0-9]{8}\.[A-Za-z0-9]{3}' } |
                        Select-Object -ExpandProperty Name
        # do something with this variable
       Write-Host "C:\Users\$userName\AppData\Local\Apps\2.0\$folderName"
    }