5.2.CMD脚本(BAT)与PowerShell脚本(PS1)
⼀、BAT ⽂件详解
1. 什么是 BAT ⽂件?
- 定义:BAT ⽂件(批处理⽂件)是⼀种以
.bat 或 .cmd 作为扩展名的脚本⽂件,基于 Windows 命令提⽰符(CMD)运⾏。
- 作⽤:
- 批量执⾏多条 CMD 命令。
- 实现简单的任务⾃动化。
- 通常⽤于简单的⽂件操作、系统管理和任务调度。
2. BAT ⽂件的特点
- 基于纯⽂本命令。
- 简单易⽤,适合快速搭建脚本。
- 不⽀持复杂数据处理和对象操作。
- 仅⽀持 Windows,⽆跨平台能⼒。
3. BAT ⽂件常⽤语法和命令
基本语法
1. 注释:以 :: 或 REM 开头。
2. 变量:
1 2
| set name=John echo Hello, %name%!
|
3. 条件语句:IF
1 2 3 4 5
| if exist file.txt ( echo File exists. ) else ( echo File not found. )
|
4. 循环语句:FOR
1 2 3
| for %%i in (*.txt) do ( echo %%i )
|
常⽤命令
| 命令 |
作用 |
示例 |
echo |
输出文本到控制台 |
echo Hello World! |
pause |
暂停,等待用户按键 |
pause |
cls |
清屏 |
cls |
dir |
列出当前目录内容 |
dir |
cd |
切换目录 |
cd C:\Users |
del |
删除文件 |
del file.txt |
copy |
复制文件 |
copy source.txt dest.txt |
move |
移动文件 |
move file.txt folder\ |
ping |
测试网络连接 |
ping www.google.com |
4. BAT ⽂件实例
1. 创建⽂件并写⼊内容
1
| echo Hello, this is a BAT file. > file.txt
|
作用:创建 file.txt 并写入一行文本。
2. ⽂件检查
1 2 3 4 5
| if exist file.txt ( echo File exists. ) else ( echo File does not exist. )
|
作用:检查 file.txt 是否存在,并输出对应提示。
3. 批量重命名⽂件
1 2 3
| for %%f in (*.txt) do ( ren "%%f" "renamed_%%f" )
|
作用:将当前目录下的 .txt 文件重命名为 renamed_文件名 。
4. ⾃动备份
1
| xcopy C:\source C:\backup /s /e /y
|
作用:将 C:\source 文件夹备份到 C:\backup 。
5. 简单菜单
1 2 3 4 5 6 7 8 9 10 11 12 13
| @echo off echo 1. Backup files echo 2. Delete temporary files set /p choice=Choose an option: if "%choice%"=="1" ( echo Backing up files... xcopy C:\source C:\backup /s /e /y ) else if "%choice%"=="2" ( echo Deleting temp files... del C:\Temp\*.* ) else ( echo Invalid choice. )
|
作用:提供一个简单的交互菜单,用户可以选择操作。
⼆、PS1 ⽂件详解
1. 什么是 PS1 ⽂件?
- 定义:PS1 ⽂件是基于 PowerShell 脚本语⾔的脚本⽂件,以
.ps1 为扩展名。
- 作⽤:
- 实现复杂的任务⾃动化。
- ⽀持跨平台操作(Windows、Linux 和 macOS)。
- 适⽤于系统管理、⽹络任务、数据处理等⾼级操作。
2. PS1 ⽂件的特点
- ⾯向对象:输出是 .NET 对象,⽀持操作属性和⽅法。
- 强⼤的逻辑和模块⽀持:⽀持函数、模块、⾃定义命令等。
- 跨平台:PowerShell Core(7.x)⽀持多操作系统。
- 安全性:默认情况下,脚本运⾏受执⾏策略限制(需要调整
ExecutionPolicy)。
注意:因为 PowerShell 默认是没有开启脚本权限的,所以需要先执⾏⼀下 Get-ExecutionPolicy 来看⼀眼权限,然后使⽤ Set-ExecutionPolicy 这个命令来临时给⼀下权限(只在当前窗⼝有效,关闭后需要重新给权限,这样会⽐较安全)。
Restricted:默认的设置,不允许任何 script 运⾏。
AllSigned:只能运⾏经过数字证书签名的 script。
RemoteSigned:运⾏本地的 script 不需要数字签名,但是运⾏从⽹络上下载的 script 就必须要有数字签名。
Unrestricted:允许所有的 script 运⾏。
权限设置示例:
1 2
| Get-ExecutionPolicy Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted
|
3. PS1 ⽂件常⽤语法和命令
基本语法
1. 注释:
2. 变量:
1 2
| $name = "John" Write-Output "Hello, $name!"
|
3. 条件语句:if-else
1 2 3 4 5
| if (Test-Path file.txt) { Write-Output "File exists." } else { Write-Output "File does not exist." }
|
4. 循环语句:foreach
1 2 3
| foreach ($i in 1..5) { Write-Output "Iteration $i" }
|
常⽤命令
| 命令 |
作用 |
示例 |
Write-Output |
输出文本到控制台 |
Write-Output "Hello World!" |
Set-Content |
创建文件并写入内容 |
Set-Content -Path file.txt -Value "Hello" |
Add-Content |
追加内容到文件 |
Add-Content -Path file.txt -Value "World" |
Remove-Item |
删除文件 |
Remove-Item -Path file.txt |
Copy-Item |
复制文件 |
Copy-Item -Path source.txt -Destination dest.txt |
Move-Item |
移动文件 |
Move-Item -Path file.txt -Destination folder |
Test-Path |
检查文件或路径是否存在 |
Test-Path -Path file.txt |
Get-Content |
显示文件内容 |
Get-Content -Path file.txt |
4. PS1 ⽂件实例
1. 创建⽂件并写⼊内容
1 2
| Set-Content -Path file.txt -Value "This is PowerShell." Add-Content -Path file.txt -Value "Another line."
|
作用:创建文件并写入多行内容。
2. 检查⽂件是否存在
1 2 3 4 5
| if (Test-Path -Path file.txt) { Write-Output "file.txt exists." } else { Write-Output "file.txt does not exist." }
|
作用:检查文件是否存在并输出结果。
3. 批量重命名⽂件
1 2 3
| Get-ChildItem -Path . -Filter "*.txt" | ForEach-Object { Rename-Item -Path $_.FullName -NewName ("new_" + $_.Name) }
|
作用:将当前目录下所有 .txt 文件重命名为 new_文件名 。
4. ⾃动备份
1
| Copy-Item -Path C:\source\* -Destination C:\backup -Recurse
|
作用:将 C:\source 下的所有文件和文件夹递归复制到 C:\backup 。
5. 下载⽂件
1
| Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"
|
作用:从指定 URL 下载文件。
6. 调⽤ REST API
1 2
| $response = Invoke-RestMethod -Uri "https://api.example.com/data" -Method Get Write-Output $response
|
作用:调用 REST API 并处理返回的数据。
总结对比
| 特性 |
BAT 文件 |
PS1 文件 |
| 功能复杂度 |
简单 |
支持高级逻辑、面向对象 |
| 跨平台 |
不支持,仅限 Windows |
支持 Windows、Linux 和 macOS |
| 适用场景 |
基础任务自动化、批处理 |
高级任务、网络操作、复杂脚本开发 |
| 安全性 |
直接执行,无权限限制 |
默认权限限制(需设置策略) |