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
相关推荐
学习论之费曼学习法2 天前
无人值守AI内容矩阵实战(10):Windows 上让 Python 进程 7×24 活着
人工智能·windows·python·矩阵·powershell·进程守护
JoePotter3 天前
从零搭建华为云 EVS 每日自动快照:hcloud + PowerShell + 邮件报告实战
华为云·自动化运维·powershell·evs·koocli·快照备份·mailozaurr
Shell运维手记5 天前
Shell 结构化命令知识点总结(case、for、while)
运维·microsoft·shell·脚本
JoePotter7 天前
Restic 备份工具使用指南(Windows Server 2019 适配版)
powershell·数据备份·windows server·运维自动化·restic
DogDaoDao10 天前
Windows 双 Shell 全面对比:CMD 与 PowerShell 的技术剖析
windows·shell·测试·powershell·cmd·程序员开发
Vex2une12 天前
PowerShell 7.5新特性解析:跨平台自动化与AI集成实战指南
ai编程·powershell·安全合规·自动化脚本·.net 9·跨平台运维·ssh远程
PegasusYu16 天前
Node.js使用nedb轻量级数据库测试脚本
数据库·electron·node.js·脚本·测试·轻量级·nedb
Vex2une19 天前
PowerShell 发展史:从命令行外壳到跨平台自动化引擎
windows·自动化·azure·powershell·vs code·脚本语言·dsc
Vex2une21 天前
PowerShell ISE 到 VS Code 迁移史:从集成脚本环境到现代编辑器工作流
windows·vscode·powershell·powershell ise·powershell 扩展·powershell 7·脚本编辑器
东鹏特饮22 天前
15个实用Linux运维脚本
linux·运维·shell·脚本·redhat·常用生产脚本