ファイルがローカル(サーバーでスクリプトを実行している)なのか、リモート(別のマシン上)なのかはわかりません。ローカルの場合は、バックグラウンドジョブを使用してコマンドを実行し、クレデンシャルをStart-Jobに渡します。
$job = Start-Job { Remove-Item -LiteralPath $path -force } -cred $cred
Wait-Job $job
Receive-Job $job
リモートの場合は、リモーティングを使用してみてください。
Invoke-Command -computername servername `
-scriptblock { Remove-Item -LiteralPath $path -force } `
-Cred $cred
注:これには、リモートマシンでEnable-PSRemotingを実行する必要があります。
一般に、スクリプトに生のパスワードを入れるのは良い考えではありません。DPAPI以降を使用して暗号化された方法でパスワードを保存できます。そのユーザーアカウントのみがパスワードを復号化できます。例:
# Stick password into DPAPI storage once - accessible only by current user
Add-Type -assembly System.Security
$passwordBytes = [System.Text.Encoding]::Unicode.GetBytes("Open Sesame")
$entropy = [byte[]](1,2,3,4,5)
$encrytpedData = [System.Security.Cryptography.ProtectedData]::Protect( `
$passwordBytes, $entropy, 'CurrentUser')
$encrytpedData | Set-Content -enc byte .\password.bin
# Retrieve and decrypted password
$encrytpedData = Get-Content -enc byte .\password.bin
$unencrytpedData = [System.Security.Cryptography.ProtectedData]::Unprotect( `
$encrytpedData, $entropy, 'CurrentUser')
$password = [System.Text.Encoding]::Unicode.GetString($unencrytpedData)
$password