PowerShell常用命令

最近WebShell需要支持PowerShell环境下的文件管理,为了实现文件相关操作需要了解下相关命令。这里总结下

https://static.1991421.cn/2024/2024-03-31-114718.jpeg

压缩文件夹

1
Compress-Archive -Path '${formatted}' -DestinationPath '${zipTempPath')}'

复制文件/文件夹

1
Copy-Item -Path '${filePath}' -Destination '${dirPath}' -Recurse -Force

移动文件/文件夹

1
Move-Item -Path '${filePath}' -Destination '${targetPath}' -Force

解压ZIP包

1
Expand-Archive -Path '${path}.zip' -DestinationPath '${destinationPath}';Remove-Item -Path '${path}.zip' -Force

删除文件/文件夹

1
Remove-Item -Path '${formatted}' -Recurse -Force

创建文件夹

1
New-Item -Path '${formatted}' -ItemType Directory

重命名

1
Rename-Item -Path '${formatted}' -NewName '${newName}'

多命令执行

如果需要执行多个命令,不同于Linux下以 && 连接,powershell需要使用;

1
Move-Item -Path '${path}' -Destination '${path}.zip';Expand-Archive -Path '${path}.zip' -DestinationPath '${destinationPath}';Remove-Item -Path '${path}.zip' -Force

常见问题

w2:无法加载文件 C:\Program Files\node js\w2.ps1,因为在此系统上禁止运行脚本。

1
2
3
4
5
6
# 管理员身份运行powershell

# 检查当前的 PowerShell 脚本执行策略
get-ExecutionPolicy
# 设置 PowerShell 的执行策略为 RemoteSigned,
set-executionpolicy remotesigned

写在最后

在windows时下执行命令时有PowerShell和CMD选择,如何选择呢。可以参考微软说法,官方是提倡使用PowerShell,CMD已进入维护阶段。