It is very simple. You just need to create a new instance of System.Management.Automation.ScriptBlock and use Create() method.

PowerShell code

$scriptBlock1 =
{
    Get-ChildItem
}

Write-Verbose -Message $scriptBlock1.GetType().FullName -Verbose

$scriptBlockString = $scriptBlock.ToString()

Write-Verbose -Message $scriptBlockString.GetType().FullName -Verbose

$scriptBlock2 = [ScriptBlock]::Create($scriptBlockString)

Write-Verbose -Message $scriptBlock2.GetType().FullName -Verbose

# Execute
& $scriptBlock2

Output

VERBOSE: System.Management.Automation.ScriptBlock
VERBOSE: System.String
VERBOSE: System.Management.Automation.ScriptBlock
« Previous Next »