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