Prompt for user input in PowerShell

Read-Host is a simple option for getting string input from a user. $name = Read-Host ‘What is your username?’ To hide passwords you can use: $pass = Read-Host ‘What is your password?’ -AsSecureString To convert the password to plain text: [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)) As for the type returned by $host.UI.Prompt(), if you run the code at … Read more

Function return value in PowerShell

PowerShell has really wacky return semantics – at least when viewed from a more traditional programming perspective. There are two main ideas to wrap your head around: All output is captured, and returned The return keyword really just indicates a logical exit point Thus, the following two script blocks will do effectively the exact same … Read more

How can I run PowerShell with the .NET 4 runtime?

The best solution I have found is in the blog post Using Newer Version(s) of .NET with PowerShell. This allows powershell.exe to run with .NET 4 assemblies. Simply modify (or create) $pshome\powershell.exe.config so that it contains the following: <?xml version=”1.0″?> <configuration> <startup useLegacyV2RuntimeActivationPolicy=”true”> <supportedRuntime version=”v4.0.30319″/> <supportedRuntime version=”v2.0.50727″/> </startup> </configuration> Additional, quick setup notes: Locations and … Read more

How do I capture the output into a variable from an external process in PowerShell?

Note: The command in the question uses Start-Process, which prevents direct capturing of the target program’s output. Generally, do not use Start-Process to execute console applications synchronously – just invoke them directly, as in any shell. Doing so keeps the application connected to the calling console’s standard streams, allowing its output to be captured by … Read more

How to output something in PowerShell

Simply outputting something is PowerShell is a thing of beauty – and one its greatest strengths. For example, the common Hello, World! application is reduced to a single line: “Hello, World!” It creates a string object, assigns the aforementioned value, and being the last item on the command pipeline it calls the .toString() method and … Read more