1

I am using Invoke-Command { & "powershell.exe" } -NoNewScope and getting error as belove.

powershell.exe : Loading personal and system profiles took 1761ms.
At line:1 char:18
+ Invoke-Command { & "powershell.exe" } -NoNewScope
+                  ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Loading persona...es took 1761ms.:String 
   ) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Error Image

  • 1
    Invoke-Command (PowerShell Remoting command) is used for running commands on a remote host. What are you trying to accomplish by trying to do this on your local machine? Without a target host, you are trying to run PS from within PS. You can do this, but , again, why? Where are you running this from? – postanote Sep 05 '22 at 06:53
  • I want to restart powershell without opening new console and I am using local machine. – Siddharth Simediya Sep 05 '22 at 06:58
  • OK, it will do that by default each time you launch PS, as long as the paths are correct and the profile(s) exist. From your error, it is trying to load a profile, thus the error is in the profile. If you launch a new instance of PS with no profile and try this, and it works, then that point to something in your profile as your choke point. If you are on Win10 or higher, you can enable the Windows Sandbox to prove this. Yet, simply closing and launching PS, will, of course, load your profile again. – postanote Sep 05 '22 at 07:02
  • Remember what [-NoNewScope](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2) is specifically designed to do. As per MS docs: ***-NoNewScope Indicates that this cmdlet runs the specified command in the current scope. By default, Invoke-Command runs commands in their own scope. This parameter is valid only in commands that are run in the current session, that is, commands that omit both the ComputerName and Session parameters.*** – postanote Sep 05 '22 at 07:06
  • 1
    You still have not said where you are running this. In the console host, this will work as I've noted. IN the ISE it will fail because that console in the ISE is not a true PowerShell console. It's an output window, that you can do interactive stuff in, but when it comes to external commands (any *.exe, etc...), then you must pass it all it needs, to get a result, You cannot use interactive commands from external *.exe in the ISE as you can in the console host. This is by design and fully documented in the MS docs. In the ISE, you use the ***New PowerShell Tab*** option in the menu. – postanote Sep 05 '22 at 07:15
  • Yes, I was working in ISE and it is working for another window. Thanks for the solution. – Siddharth Simediya Sep 05 '22 at 07:38

1 Answers1

1

This is...

Invoke-Command { & "powershell.exe" } -NoNewScope

... is for the console host based on your use case.

If you are in the ISE, enter image description here

... you must use New PowerShell Tab option, to get a new session and your profile will load there.

enter image description here

You can use the shortcut key of course. CRTL+T

enter image description here

Differences between the ISE and PowerShell console - PowerShell Team

Console Application (Non) Support in the ISE

$psUnsupportedConsoleApplications
# Results
<#
wmic
wmic.exe
cmd
cmd.exe
diskpart
diskpart.exe
edit.com
netsh
netsh.exe
nslookup
nslookup.exe
powershell
powershell.exe
ssh-keygen
ssh-keygen.exe
#>

PowerShell- Running Executables - TechNet Articles - United States (English) - TechNet Wiki

postanote
  • 15,138
  • 2
  • 14
  • 25
  • I got your point. Thanks for giving your time for elaboration. – Siddharth Simediya Sep 05 '22 at 07:40
  • No worries. Many get struck by the console in the ISE. Back in the day, the ISE had 3 panes; the editor, the outp[ut window, and the console. Why Microsoft removed the console in the later ISE versions, who knows; I know I was disappointed. – postanote Sep 05 '22 at 07:48