powershell 遍历文件夹
时间: 2023-05-30 20:05:34 浏览: 859
使用 PowerShell 遍历文件夹可以使用 `Get-ChildItem` 命令。以下是一个简单的例子:
```powershell
Get-ChildItem -Path "C:\Users\Username\Documents" -Recurse
```
这个命令将遍历 `C:\Users\Username\Documents` 文件夹及其所有子文件夹,并返回所有文件和文件夹的列表。
您可以使用 `-Filter` 参数来过滤结果,例如:
```powershell
Get-ChildItem -Path "C:\Users\Username\Documents" -Recurse -Filter "*.txt"
```
这个命令将仅返回 `.txt` 文件的列表。
您可以使用 `-Exclude` 参数来排除特定类型的文件,例如:
```powershell
Get-ChildItem -Path "C:\Users\Username\Documents" -Recurse -Exclude "*.txt"
```
这个命令将返回除 `.txt` 文件之外的所有文件和文件夹的列表。
您还可以使用 `-File` 参数仅返回文件,或使用 `-Directory` 参数仅返回文件夹,例如:
```powershell
Get-ChildItem -Path "C:\Users\Username\Documents" -Recurse -File
```
这个命令将仅返回文件的列表。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![vsdx](https://img-home.csdnimg.cn/images/20210720083646.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)