You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell密码生成脚本如何自定义长度、字符集并支持默认规则

调整后支持自定义配置的实现方案
function New-RandomPassword {
    [CmdletBinding()]
    param(
        # 自定义密码长度,默认8位
        [Parameter()]
        [int]$PasswordLength = 8,

        # 自定义小写字母可用字符集,默认过滤易混淆字符
        [Parameter()]
        [string]$LowercaseChars = 'abcdefghiklmnoprstuvwxyz',

        # 自定义大写字母可用字符集,默认过滤易混淆字符
        [Parameter()]
        [string]$UppercaseChars = 'ABCDEFGHKLMNOPRSTUVWXYZ',

        # 自定义数字可用字符集,默认过滤易混淆字符
        [Parameter()]
        [string]$NumberChars = '1234567890',

        # 自定义特殊字符可用字符集
        [Parameter()]
        [string]$SpecialChars = '!"§$%&/()=?}][{@#*+',

        # 统一自定义全局字符集,传值后会忽略上述四类字符集配置
        [Parameter()]
        [string]$CustomCharset = ''
    )

    # 辅助函数:从指定字符集取指定长度随机字符
    function Get-RandomCharacters($length, $characters) {
        $random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
        $private:ofs = ""
        return [String]$characters[$random]
    }
 
    # 辅助函数:打乱字符串顺序
    function Scramble-String([string]$inputString){     
        $characterArray = $inputString.ToCharArray()   
        $scrambledStringArray = $characterArray | Get-Random -Count $characterArray.Length     
        $outputString = -join $scrambledStringArray
        return $outputString 
    }

    $password = ""
    # 优先使用自定义全局字符集
    if(-not [string]::IsNullOrEmpty($CustomCharset)){
        $password = Get-RandomCharacters -length $PasswordLength -characters $CustomCharset
        return Scramble-String $password
    }

    # 收集非空的分类字符集
    $enabledCharsets = @($LowercaseChars, $UppercaseChars, $NumberChars, $SpecialChars) | Where-Object { -not [string]::IsNullOrEmpty($_) }
    if($enabledCharsets.Count -eq 0){
        Write-Error "未指定任何可用字符集,请检查参数配置"
        return $null
    }

    # 长度足够的情况下保证每类启用的字符集至少有1位
    if($PasswordLength -ge $enabledCharsets.Count){
        $remainingLength = $PasswordLength - $enabledCharsets.Count
        # 每类先取1位
        foreach($charset in $enabledCharsets){
            $password += Get-RandomCharacters -length 1 -characters $charset
        }
        # 剩余长度从所有启用字符的合并集取
        $allCharset = $enabledCharsets -join ""
        $password += Get-RandomCharacters -length $remainingLength -characters $allCharset
    }
    else{
        # 长度不足时直接从合并字符集随机取
        $allCharset = $enabledCharsets -join ""
        $password = Get-RandomCharacters -length $PasswordLength -characters $allCharset
    }

    # 打乱字符顺序后返回
    return Scramble-String $password
}
使用示例
  • 直接调用默认配置生成8位密码:
    New-RandomPassword
  • 生成长度为12位的默认规则密码:
    New-RandomPassword -PasswordLength 12
  • 禁用特殊字符,生成10位仅包含大小写和数字的密码:
    New-RandomPassword -PasswordLength 10 -SpecialChars ""
  • 自定义仅使用指定字符生成6位密码:
    New-RandomPassword -CustomCharset "abcdef123456!@#" -PasswordLength 6
  • 替换默认特殊字符集,仅使用!@#$%作为特殊字符:
    New-RandomPassword -SpecialChars "!@#$%"
功能说明
  • 所有参数都预设了默认值,未传参时会沿用原脚本的生成规则,输出8位包含大小写、数字、特殊字符的密码
  • 支持灵活调整字符范围,可单独禁用某类字符,也可直接传入统一字符集完全自定义生成规则
  • 长度足够的情况下会默认保证每类启用的字符集至少出现1位,符合绝大多数系统的密码复杂度要求
  • 最终输出前会打乱所有字符的顺序,不会出现固定位置的字符类型,避免降低密码安全性

内容的提问来源于stack exchange,提问作者Vadim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 01:27:03