Getting the name of your PC or VM in PowerShell is easy! You can use this in the PowerShell console or in your scripts if you need to get the machine’s name.
$env:COMPUTERNAME
The response will be the name of your computer or the name of your VM (Virtual Machine). You can also store this in a $variable
.