Powershell - IO.Directory - Find file types in all subdirectories

前端 未结 3 1314
天命终不由人
天命终不由人 2021-01-13 15:20

I ran across this code in another post that almost does what I need, but can\'t figure out how to modify it to look for specific file types, i.e. *.bak, .txt, etc. I\'m

3条回答
  •  甜味超标
    2021-01-13 16:08

    This will loop through each extension searching for all files in the root and sub-directories. Ensure you have the correct privileges on all the directories especially when you're running from C:\ as the root.

    $Extensions = @(".bak",".csv",".txt")
    
    Foreach ( $Extension in $Extensions )
    {
       [System.IO.Directory]::EnumerateFiles("C:\","*$Extension","AllDirectories")
    }
    

    This method will only work with Powershell running .Net 4.0 or higher. To check and update the version of .Net:

    $PSVersionTable
    
    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.4971
    BuildVersion                   6.1.7600.16385
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    PSRemotingProtocolVersion      2.1
    

    The CLRVersion value is the .net version.

    Update the config file as follows:

    $Config = @"
    
    
        
            
            
        
    
    "@
    
    $Config > $PSHOME\Powershell.exe.config
    

    Restart the Powershell session and verify the CLRVersion value in the $PSVersionTable variable.

提交回复
热议问题