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
相关推荐
winfredzhang3 天前
用 wxPython 打造「Claude Code 任务启动器」:一个把 AI CLI 变成一键工作流的桌面工具
python·wxpython·powershell·剪贴板·claudecodecli
鬼手点金4 天前
Hermes‑Agent 使用教程
python·ubuntu·powershell·cmd·ai agent·opencode·hermes
寒水馨9 天前
macOS下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-osx-arm64.pkg)
macos·自动化·跨平台·shell·脚本·命令行·powershell
寒水馨10 天前
Linux下载、安装PowerShell-v7.6.4(附安装包powershell-7.6.4-linux-x64.tar.gz)
linux·跨平台·自动化运维·devops·命令行·powershell·脚本语言
逻极16 天前
Shell脚本实战:从“能跑”到“高效”的3个关键跃升
linux·服务器·shell·脚本
至乐活着25 天前
Shell脚本编写最佳实践:打造健壮、可维护的自动化利器
自动化·bash·shell·脚本·最佳实践
儒雅的烤地瓜1 个月前
计算机网络 | 网络诊断双刃剑:Tracert路由追踪实战与Windows防火墙端口配置指南
tcp/ip·计算机网络·wireshark·防火墙·powershell·cmd
vortex51 个月前
PowerShell Provider 机制详解:从文件系统到注册表的统一抽象
windows·powershell
xcLeigh1 个月前
KES运维自动化与脚本体系实战
运维·数据库·自动化·脚本·数据迁移·kes
CAU界编程小白1 个月前
CAU抢课脚本
c++·脚本