如何获取所有用户配置文件中不同的文件夹名称

How to get folder name thats different on all user profiles

我想知道如何让 5J91Q4CX.C10 在变量中使用。

C:\Users\user\AppData\Local\Apps.0J91Q4CX.C10

在所有用户配置文件中,此文件夹具有不同的名称。 它始终是 8 个数字和数字,然后是 .,然后是 3 个数字或数字。

我需要将其用于 powershell 脚本。

知道如何为这个文件夹名创建一个变量吗? 谢谢

一些 RegEx 可以做到这一点:

$str = "C:\Users\user\AppData\Local\Apps.0J91Q4CX.C10"
$str -match '.*\(.*)$'

$matches[1] # 5J91Q4CX.C10

.*\(.*)$ 匹配最后一个破折号 \ 之后和行尾 $

之前的所有字符

不确定您真正想做什么...您可以通过 C:\Users 进行目录搜索以报告所有子文件夹,然后使用 Foreach 循环遍历每个子文件夹并创建所需的文件在目的地等中,类似于:

$FOLDERS = Get-ChildItem C:\Users -Directory

FOREACH ($FOLDER in $FOLDERS) {
#WHATEVER YOU WANT TO DO
}

我会这样做:

#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.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.0$folderName"
}