How can I start powershell.exe process using some line separator?

I want to run powershell.exe using this script (it works fine):

 Start-Process powershell.exe { Get-Help Get-Process } 

But this script does not work:

 Start-Process powershell.exe { $string = "123xAbcxEFG" $split1,$split2,$split3 = $string.Split("x") Write-Output $split1 Write-Output $split2 Write-Output $split3 sleep 10 } 

I think I need Add-Type -AssemblyName "SomeNameSpace" , but how can I find this namespace? Any intellisense or something like that?

0
source share
3 answers

you can use start-job, which actually starts a new powershell process. and you can do it easily. eg:

 start-job 

for more help:

 get-help start-job 
+1
source

The problem with quotation marks. It works if you, for example. add extra single quotes around your double quotes. It also works with triple double quotes.

 Start-Process powershell.exe { $string = """123xAbcxEFG""" $split1,$split2,$split3 = $string.split("""x""") Write-Output $split1 Write-Output $split2 Write-Output $split3 sleep 10 } 

How I changed the code to catch an error (without extra double quotes):

 $ScriptBlock = { $string = "123xAbcxEFG" $split1,$split2,$split3 = $string.split("x") Write-Output $split1 Write-Output $split2 Write-Output $split3 sleep 10 } Start-Process powershell -argumentlist "-noexit -command $ScriptBlock" 
+2
source

you can use start-job, which actually starts a new powershell process. and you can do it easily. eg:

 $script= {get-help "dir"} start-job -scriptblock $script 
0
source

Source: https://habr.com/ru/post/1011889/


All Articles