'how to run powershell command from nushell
After installing nushell (https://www.nushell.sh/) in my windows environment, I want to use the power of nushell in conjunction with powershell.
For example, I need to invoke common powershell commands in my daily work like
> get-service *myservice -computername remote | restart-service
But how to invoke such command (with pipeline) from nu shell?
Do I need to start nu shell from powershell or vica versa? Also the invocation with the ^ sign seems to not work. I always get "command not found" and for sure it is no DOS command.
It would be nice to get the power from both worlds on windows ... nu shell and powershell
Solution 1:[1]
You can run a one-off command in PowerShell using the -command
flag.
So from Nu (or cmd.exe or any other shell), something like this will work:
pwsh -command "echo 'Hello from PowerShell'"
(you may need to replace pwsh
with powershell
if you want an older Windows Powershell version)
Solution 2:[2]
Given your example, I'm not sure there's much there that you could use to combine PowerShell and Nushell. In that particular case, you'd just run the PowerShell command from inside Nushell as mentioned in the previous answer, such as:
pwsh -c "get-service *myservice -computername remote | restart-service"
However, let's say that you want to convert structured PowerShell output to structured Nushell objects. You can use JSON for that purpose. For example, in Nu:
(pwsh.exe -c "Get-Service -ErrorAction Ignore | ConvertTo-Json -WarningAction Ignore") | from json | where Status == 1
Note, however, that you may pretty quickly run into the issue that I describe in this Unix & Linux question, in that JSON results typically end up as a Nushell list rather than a table.
It's possible to start the pipeline from either inside Nu, calling PowerShell, or vice-versa.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Reilly Wood |
Solution 2 |