There is a possible answer to this at the end, but a bit of background may help so I'm starting with that.
Intro
It is important to note that my answer is only related to loopback (localhost
) sessions. It does not address problems with remote sessions.
Loopback sessions are useful as they enable a user with administrator rights to invoke user commands or scripts on the local host. An example of this is a toast message to a logged in user from a system monitoring program run under the System user. (Yes, there may be better ways of doing this but that's not what is being discussed here).
Background
I had exactly the same problem with exactly the same results as what is shown in the updates section of @Piotr's original question. I searched extensively on Google for an answer, but this yielded no working answers—even though a few people were reporting very similar issues.
Specs
As most people using Powershell remoting seem to not have this problem, it may be related to my machine's specs:
- Windows 10 Home Version 1803.
- Private network using WiFi connection
PS Env:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17134.858
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17134.858
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Possible Answer
The EnableNetworkAccess
option worked for me, but I don't know why yet. It is confirmed to work for the PowerShell commands New-PSSession
and Invoke-Command
, and may work for others as well.
Examples
To configure PowerShell remoting, the following command is needed once—though it
will not harm anything if it is repeated. It will only work on private (not public) networks.
PS Env:\> Enable-PSRemoting -force
The following examples don't require an entry in Trusted Hosts.
LAPTOP-XZ
is the computer name == $env:COMPUTERNAME
.
= dot alias for localhost
Invoke-Command -ComputerName . -ScriptBlock { dir } -EnableNetworkAccess
New-PSSession -EnableNetworkAccess
New-PSSession localhost -EnableNetworkAccess
New-PSSession 127.0.0.1 -EnableNetworkAccess
New-PSSession LAPTOP-XZ -EnableNetworkAccess
The IPv4 Address of the computer (192.168.1.103
, in my case) will only work if it is included in Trusted Hosts. The EnableNetworkAccess
option isn't needed in this case.
PS Env:\> Set-Item WSMan:\localhost\Client\TrustedHosts -Value "192.168.1.103"
New-PSSession 192.168.1.103
Note that including localhost
and its other aliases in Trusted Hosts doesn't work in the above example (-EnableNetworkAccess
is needed).