Powershell 命令实操
PowerShell 是一种功能强大的命令行外壳和脚本语言,以下是一些常见的 PowerShell 命令:
Get-ChildItem:类似于 CMD 中的dir命令,用于获取指定位置的文件和文件夹列表。
例如:
Get-ChildItem -Path C:\Program Files
将列出C:\Program Files目录下的内容。
Set-Location:相当于 CMD 中的cd命令,用于更改当前工作目录。
例如
Set-Location -Path C:\Users\Admin
会将当前目录切换到C:\Users\Admin。
New-Item:可用于创建新的文件或文件夹。例如,
New-Item -ItemType Directory -Path C:\NewFolder
将在C:\下创建一个名为NewFolder的新文件夹;
New-Item -ItemType File -Path C:\NewFile.txt
则会创建一个名为NewFile.txt的新文件。
Remove-Item:用于删除文件或文件夹。
例如
Remove-Item -Path C:\OldFile.txt
将删除C:\下的OldFile.txt文件;
若要删除文件夹及其内容,可使用
Remove-Item -Path C:\OldFolder -Recurse
其中-Recurse参数表示递归删除文件夹中的所有内容。
Copy-Item:用于复制文件或文件夹。例如,
Copy-Item -Path C:\SourceFile.txt -Destination C:\Backup\SourceFile.txt
将把C:\下的SourceFile.txt文件复制到C:\Backup目录下。
Move-Item:用于移动文件或文件夹,也可用于重命名。
例如,
Move-Item -Path C:\Temp\File.txt -Destination C:\Final\File.txt
将把文件从C:\Temp移动到C:\Final目录;
Move-Item -Path C:\OldFolder -Destination C:\NewFolder
可将C:\下的OldFolder重命名为NewFolder。
Test-Connection:类似于 CMD 中的ping命令,用于测试网络连接。例如,
Test-Connection -ComputerName www.baidu.com
可以检查与百度服务器的连接情况。
Get-NetIPConfiguration:用于获取网络接口的 IP 配置信息,包括 IP 地址、子网掩码、默认网关等,类似于 CMD 中的ipconfig命令。
Get-NetIPConfiguration
Get-Process:用于获取当前正在运行的进程信息。例如,Get-Process -Name Notepad可以获取记事本程序的进程信息,如果记事本正在运行的话。
Get-Process
Stop-Process:用于停止指定的进程。例如,Stop-Process -Name Notepad将停止正在运行的记事本程序。
Stop-Process
11.在 PowerShell 中,你可以使用Get-ChildItem和Remove-Item命令来递归删除空文件夹,示例如下:
Get-ChildItem -Directory -Recurse | Where-Object { $_.GetDirectories().Count -eq 0 -and $_.GetFiles().Count -eq 0 } | Remove-Item