【问题标题】:How to get folder name thats different on all user profiles如何在所有用户配置文件上获取不同的文件夹名称
【发布时间】:2018-11-21 16:04:39
【问题描述】:

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

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

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

我需要将它用于 powershell 脚本。

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

【问题讨论】:

    标签: powershell variables appdata


    【解决方案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"
    }
    

    【讨论】:

      【解决方案2】:

      一些RegEx 可以解决问题:

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

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

      【讨论】:

        【解决方案3】:

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

        $FOLDERS = Get-ChildItem C:\Users -Directory
        
        FOREACH ($FOLDER in $FOLDERS) {
        #WHATEVER YOU WANT TO DO
        }
        

        【讨论】:

          猜你喜欢
          • 2016-12-24
          • 1970-01-01
          • 2018-04-02
          • 1970-01-01
          • 1970-01-01
          • 2019-05-29
          • 1970-01-01
          • 2014-07-30
          • 1970-01-01
          相关资源
          最近更新 更多