save PSCredential in the file

Update on non-Windows Platforms A lot has changed since this answer was first written. Modern versions of PowerShell are based on .net core, and run cross-platform. The underlying type that enables this whole answer is called [securestring] and the security and encryption that backs it comes from the Data Protection API (DPAPI) on Windows, which … Read more

Pass arguments to a scriptblock in powershell

Keith’s answer also works for Invoke-Command, with the limit that you can’t use named parameters. The arguments should be set using the -ArgumentList parameter and should be comma separated. $sb = { param($p1,$p2) $OFS=’,’ “p1 is $p1, p2 is $p2, rest of args: $args” } Invoke-Command $sb -ArgumentList 1,2,3,4 Also see here and here.

INI file parsing in PowerShell

After searching internet on this topic I’ve found a handful of solutions. All of them are hand parsing of file data so I gave up trying to make standard cmdlets to do the job. There are fancy solutions as this which support writing scenario. There are simpler ones and as far as I need no … Read more

Lambda Expression in Powershell

In PowerShell 2.0 you can use a script block ({ some code here }) as delegate: $MatchEvaluator = { param($m) if ($m.Groups[“val”].Value -eq “;”) { #… } } $result = $r.Replace($input, $MatchEvaluator) Or directly in the method call: $result = $r.Replace($input, { param ($m) bla }) Tip: You can use [regex] to convert a string … Read more

Executing an EXE file using a PowerShell script

& “C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe” C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode or [System.Diagnostics.Process]::Start(“C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe”, “C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode”) UPDATE: sorry I missed “(I invoked the command using the “&” operator)” sentence. I had this problem when I was evaluating the path dynamically. Try Invoke-Expression construction: Invoke-Expression “& `”C:\Program Files\Automated QA\TestExecute 8\Bin\TestExecute.exe`” C:\temp\TestProject1\TestProject1.pjs /run /exit /SilentMode”