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

时间:2018-11-21 16:04:39

标签: powershell variables appdata

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

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

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

我需要在Powershell脚本中使用它。

您知道如何为该文件夹名称创建变量吗? 谢谢

3 个答案:

答案 0 :(得分:1)

某些RegEx可以达到目的:

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

$matches[1] # 5J91Q4CX.C10

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

之前的所有字符。

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

我会做这样的事情:

#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"
}
相关问题