Powershell笔记

操作符

变量前面加$

参数前面加-

注释前面加#

代码结尾无分号

逻辑运算符

比大小

  • -eq‌:等于
  • ‌**-ne**‌:不等于
  • ‌**-gt**‌:大于
  • ‌**-ge**‌:大于等于
  • ‌**-lt**‌:小于
  • ‌**-le**‌:小于等于

布尔运算

-and -or -not -xor

数据类型(常用)

int

float

string使用单引号或双引号定义,双引号支持变量插值

判断

复制代码
if(判断语句){

}

循环

for循环

bash 复制代码
for ($i = 1; $i -le 10; $i++) {
    Write-Host "当前数字: $i"
}

foreach循环

复制代码
foreach ($XXX in $XXXs){
    
}

while循环

bash 复制代码
$count = 1
while ($count -le 5) {
    Write-Host "计数: $count"
    $count++
}

函数

函数定义

复制代码
function 函数名{
    param(
        [类型]$参数名1,
        [类型]$参数名2,
        [类型]$参数名3=默认值
    )
    函数体
}

函数调用

复制代码
函数名 -参数名1 值 -参数名2 值 -参数名3 值

常用API

Get-ChildItem得到文件夹成员

复制代码
Get-ChildItem -Path 路径 -Filter "*.后缀名"
#路径写.代表脚本所在的路径

Write-Host打印

复制代码
Write-Host "提示语" -ForegroundColor 颜色

ChangeExtension改后缀名

复制代码
[System.IO.Path]::ChangeExtension($file.Name, ".jpg")

$PSScriptRoot当前脚本的目录

报错

对webp使用System.Drawing.Image::FromFile($_.FullName)提示内存不足

System.Drawing.Image不支持webp。

一些脚本

调整jpg的质量

在倒数第2行的-quality后面输入百分比质量,结果在New文件夹。

复制代码
function ChangeImageQuality{
    param (
        [string]$imagePath,
        [string]$outputPath,# 设置输出路径和新的图像
        [ValidateRange(0, 100)]
        [int]$quality = 75# 质量范围从1到100,100为最高
    )
    Add-Type -AssemblyName System.Drawing
    # 加载图像
    $image = [System.Drawing.Image]::FromFile($imagePath)

    # 创建一个JPEG编码器的实例
    $encoder = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq "image/jpeg" }
    $encoderParams = New-Object System.Drawing.Imaging.EncoderParameters(1)
    $encoderParams.Param = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, $quality)

    # 保存图像
    $image.Save($outputPath, $encoder, $encoderParams)

    # 清理资源
    $image.Dispose()
    $imagePath+"已修改完成"
}
$path=$PSScriptRoot
$newPath=$path+"\New"
if (-not (Test-Path -Path $newPath)) {
    # 文件夹不存在,创建文件夹
    New-Item -ItemType Directory -Path $newPath
    Write-Host "创建了文件夹$newPath"
    }
$files = Get-ChildItem -Path $path -Filter "*.jpg"
foreach($file in $files) {
#file.Name是完整文件名,BaseName是不包括扩展名的文件名
    #$file.Name
    $newName=$newPath+"\"+$file.BaseName+"_1.jpg"
    ChangeImageQuality -imagePath $path"\"$file -outputPath $newName -quality 70
}

删除文件夹里某类型文件

这里是webp

复制代码
Get-ChildItem -Path "." -Recurse -Filter "*.webp" | Remove-Item -Force
相关推荐
至乐活着5 天前
Shell脚本编写最佳实践:打造健壮、可维护的自动化利器
自动化·bash·shell·脚本·最佳实践
儒雅的烤地瓜5 天前
计算机网络 | 网络诊断双刃剑:Tracert路由追踪实战与Windows防火墙端口配置指南
tcp/ip·计算机网络·wireshark·防火墙·powershell·cmd
vortex514 天前
PowerShell Provider 机制详解:从文件系统到注册表的统一抽象
windows·powershell
xcLeigh18 天前
KES运维自动化与脚本体系实战
运维·数据库·自动化·脚本·数据迁移·kes
CAU界编程小白18 天前
CAU抢课脚本
c++·脚本
无为之士1 个月前
Windows 批量打印 PDF 工具分享:支持文件夹、指定文件、当天文件、预览列表
windows·powershell
疯狂学习GIS1 个月前
基于Python earthaccess库批量下载全球MODIS GPP(MOD17A2HGF)数据
python·脚本·批量下载·遥感影像·nasa·earthdata·自动处理
阆遤1 个月前
Windows环境安装Hermes Desktop安装
powershell·ai agent·hermes desktop
大大杰哥2 个月前
Windows 批处理语法笔记:从入门到一键部署项目
脚本·命令行
一念&2 个月前
油猴脚本教程——元数据块
javascript·浏览器·脚本·油猴