0

'Get-Command' returns a list of all available commands in a powershell session.

The following code evaluates to $false, when passed a string which is not a command:

function f($x) {[bool](Get-Command $x -ErrorAction SilentlyContinue)}

And it does:

PS C:\Windows\system32> f ls
True
PS C:\Windows\system32> f alkdsjfasd
False

So you would think that the following code would return the list of commands which are not commands -- that is an empty list:

Get-Command | Where-Object {[bool](Get-Command $_ -ErrorAction SilentlyContinue) -eq $false}

Except it doesn't. It returns the following on a clean Windows Server 2016 VM:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Get-IseSnippet                                     1.0.0.0    ISE
Function        Import-IseSnippet                                  1.0.0.0    ISE
Function        New-IseSnippet                                     1.0.0.0    ISE
Function        Start-AutologgerConfig                             1.0.0.0    EventTracingManagement
Cmdlet          Add-ClusteriSCSITargetServerRole                   2.0.0.0    IscsiTarget

If I wait a little while and run the same command again, it returns a much longer list:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Function        Add-RDServer                                       2.0.0.0    RemoteDesktop
Function        Add-RDSessionHost                                  2.0.0.0    RemoteDesktop
Function        Add-RDVirtualDesktopToCollection                   2.0.0.0    RemoteDesktop
Function        Disable-RDVirtualDesktopADMachineAccountReuse      2.0.0.0    RemoteDesktop
Function        Get-IseSnippet                                     1.0.0.0    ISE
...
Function        Test-RDVirtualDesktopADMachineAccountReuse         2.0.0.0    RemoteDesktop
Function        Update-RDVirtualDesktopCollection                  2.0.0.0    RemoteDesktop

I've checked the files in the RemoteDesktop module, for example, and I've been able to find the functions that are "missing".

What is going on here? How can 'Get-Command' return "commands" that aren't commands?

Daniel S
  • 1
  • 1
  • what happens if you run the commands after a period of time on the command line using the -noprofile flag? – Ctznkane525 Mar 27 '19 at 22:20
  • Hmmm ... I'm not sure if I got what the actual question is. `Get-Command` returns cmdlets, functions and aliasses ... all that depending on the underlying OS and installed modules and products – Olaf Mar 27 '19 at 22:21
  • @Ctznkane525 The behavior is identical whether or not I start powershell with the -NoProfile switch. The first run outputs a few commands, the second outputs many more. – Daniel S Mar 28 '19 at 01:19
  • @Olaf It's true that the output of Get-Command is different on different computers, but the output of 'gcm | where {[bool](gcm $_ -ErrorAction SilentlyContinue) -eq $false}' should always be nothing. No matter what system it's run on. It's like if I take a list of all the files in a folder, and then remove each file that's in the folder, I should be left with an empty list, no matter what folder I do that with. – Daniel S Mar 28 '19 at 01:34
  • I can't fully account for it, but `get-command` lists available commands, including modules which are not imported. `get-command $_` triggers module auto-import. For one example, the `webadministration` module on my system claims it will export `Begin-WebCommitDelay` as an alias, during import as a normal user it errors because I'm not an admin and cannot access IIS configuration data, and then whatever happens it does not export that alias at all and get-command no longer shows it. I suspect the delay of modules auto-importing, and the change of state during import is involved in this. – TessellatingHeckler Mar 28 '19 at 10:27

0 Answers0