Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 如何获取所有用户配置文件上不同的文件夹名称_Powershell_Variables_Appdata - Fatal编程技术网

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

Powershell 如何获取所有用户配置文件上不同的文件夹名称,powershell,variables,appdata,Powershell,Variables,Appdata,我想知道如何在变量中使用5J91Q4CX.C10 C:\Users\user\AppData\Local\Apps\2.0\5J91Q4CX.C10 在所有用户配置文件上,此文件夹具有不同的名称。 它始终是8个数字和数字,然后是a,然后是3个数字或数字 我需要将其用于powershell脚本 知道如何为这个foldername创建一个变量吗? 谢谢一些RegEx可以做到这一点: $str = "C:\Users\user\AppData\Local\Apps\2.0\5J91Q4CX.C10"

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

C:\Users\user\AppData\Local\Apps\2.0\5J91Q4CX.C10
在所有用户配置文件上,此文件夹具有不同的名称。 它始终是8个数字和数字,然后是a
,然后是3个数字或数字

我需要将其用于powershell脚本

知道如何为这个foldername创建一个变量吗?
谢谢

一些
RegEx
可以做到这一点:

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

$matches[1] # 5J91Q4CX.C10

*\\(.*)$
匹配最后一个破折号
\
之后和行尾之前的所有字符
$

一些
RegEx
可以实现以下功能:

$str = "C:\Users\user\AppData\Local\Apps\2.0\5J91Q4CX.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
}

不确定你到底想做什么。。。您可以通过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\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"
}

我会这样做:

#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"
}