By default Out-File
clobbers (overwrites) at the specified path if it exists. If the file doesn't exist prior script execution, use -Append
to append to the file:
Out-File .\Documents\L8_userpasswords.txt -InputObject $pwd -Append
Note that this will append to the file each time the script is run. If you want the file to be recreated each time, check existence and delete it before entering the for
loop:
$file = ".\L8_userpasswords.txt"
if (Test-Path -Path $file -PathType Leaf) {
Remove-Item $file
}
for ($i=1; $i -le 10; $i++){
...