Summary: Create a Windows PowerShell script block on the fly.
How can I convert a string into a Windows PowerShell script block?
Use the static Create method from the [scriptblock] class:
PS C:\> [scriptblock]::Create("this is a string") | gm
TypeName: System.Management.Automation.ScriptBlock
Name MemberType Definition
---- ---------- ----------
CheckRestrictedLanguage Method void CheckRestrictedLanguage(System.Collection..
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetNewClosure Method scriptblock GetNewClosure()
GetObjectData Method void GetObjectData(System.Runtime.Serializatio..
GetPowerShell Method powershell GetPowerShell(Params System.Object[..
GetSteppablePipeline Method System.Management.Automation.SteppablePipeline..
GetType Method type GetType()
Invoke Method System.Collections.ObjectModel.Collection[psob..
InvokeReturnAsIs Method System.Object InvokeReturnAsIs(Params System.O..
ToString Method string ToString()
Ast Property System.Management.Automation.Language.Ast Ast ..
Attributes Property System.Collections.Generic.List[System.Attribu..
File Property string File {get;}
IsFilter Property bool IsFilter {get;set;}
Module Property psmoduleinfo Module {get;}
StartPosition Property System.Management.Automation.PSToken StartPosi..