Is there a way of validating a powershell script?

c#, powershell

Solution

In PowerShell v2 you have tokenizer that can process script without running it. Look at class System.Management.Automation.PSParser and it's static method Tokenize:

http://msdn.microsoft.com/en-us/library/system.management.automation.psparser(v=vs.85).aspx

In v3 it's gets even better, there is whole language namespace/ AST support:

http://msdn.microsoft.com/en-us/library/system.management.automation.language(v=vs.85).aspx

HTH Bartek

Problem

My app allows users to enter powershell scripts that it will run for you later. Is there a straightforward way to be able to validate the powershell script without running it, so that when the user enters it the program could report syntax errors? Thanks.

Original source