我不能'Out-File'我的整个循环只有一行

Mus*_*usa 3 powershell powershell-ise

我创建了一个随机密码生成器,我需要将所有10个输出Out-File转换为.txt文件

但我现在只有1行输出.

for ($i=1; $i -le 10; $i++){
$caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
$lows = [char[]] "abcdefghjkmnpqrstuvwxy" 
$nums = [char[]] "2346789"
$spl = [char[]] "!@#$%^&*?+"

$first = $lows | Get-Random -count 1;
$second = $caps | Get-Random -count 1;
$third = $nums | Get-Random -count 1;
$forth = $lows | Get-Random -count 1;
$fifth = $spl | Get-Random -count 1;
$sixth = $caps | Get-Random -count 1;

$pwd = [string](@($first) + @($second) + @($third) + @($forth) + @($fifth) + @($sixth))
Write-Host $pwd

Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd

}
Run Code Online (Sandbox Code Playgroud)

当我打开.txt时,我只看到1行而不是10行.

Bry*_*yan 5

默认情况下Out-File,如果存在,则指定路径上的clobbers(覆盖).如果文件在脚本执行之前不存在,则使用-Append附加到文件:

Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd -Append
Run Code Online (Sandbox Code Playgroud)

请注意,每次运行脚本时,它都会附加到文件中.如果您希望每次都重新创建文件,请检查是否存在并在进入for循环之前将其删除:

$file = ".\L8_userpasswords.txt"
if (Test-Path -Path $file -PathType Leaf) {
    Remove-Item $file
}
for ($i=1; $i -le 10; $i++){
...
Run Code Online (Sandbox Code Playgroud)