PowerShell Repair?

Is there a way to repair (or uninstall and reinstall) PowerShell v4? I’m getting the error below when running get-command and would like to set PowerShell v4.0 back to its “factory defaults” so I can play with version 4 and in particular DSC. (.Net 4.5 Framework is installed.) Thanks.

PS Env:> $psversiontable

Name Value


PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18444
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

PS Env:> get-command
get-command : Object reference not set to an instance of an object.
At line:1 char:1

  • get-command
  •   + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
      + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand

John,

Uninstalling the Windows Management Framework 4.0 (KB2819745) and reinstalling it after a restart might work (http://www.microsoft.com/en-gb/download/details.aspx?id=40855).

What version of Windows are you using?

PowerShell 4.0 is installed by default on Windows 8.1 and Windows Server 2012 R2 - you can’t uninstall it from those machines.

Thanks Richard, Windows 7 (x64) SP1. Until recently I had PowerShell v3, and had no issues with that. I installed WMF4 (after checking I had .Net Framework 4.5 first) and only found I had an issue with PowerShell v4 when “Get-DSCResource” errored. I’m minded to follow Daniel’s advice and uninstall then reinstall WMF4 (as I’ve no idea how to get get-command working).

OK - you can uninstall/reinstall on that platform.

I’m not confident this is going to work. I uninstalled WMF4 OK. Now I can open the ISE, and see PowerShell is back to v3. But get-command gives a similar error:

PS C:\Users\John> $psversiontable
get-command
show-command

Name Value


PSVersion 3.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18444
BuildVersion 6.2.9200.16481
PSCompatibleVersions {1.0, 2.0, 3.0}
PSRemotingProtocolVersion 2.2
get-command : Object reference not set to an instance of an object.
At line:2 char:1

  • get-command
  •   + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
      + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand
    
    

get-command : Object reference not set to an instance of an object.
At line:1 char:3

  • @(get-command -CommandType Cmdlet,Function,Script,ExternalScript,Workflow),@(get …
  • + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand

Uninstalling then reinstalling WMF4 did not fix get-command, which still errors, details below. Has anyone got any ideas what I may be doing wrong? This is on Windows 7 SP1 x64.

PS C:\Users\John> $psversiontable

Name Value


PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18444
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

PS C:\Users\John> get-command
get-command : Object reference not set to an instance of an object.
At line:1 char:1

  • get-command
  •   + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
      + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand

This bit looks odd in the error you posted last time:

@(get-command -CommandType Cmdlet,Function,Script,ExternalScript,Workflow),@(get …

That doesn’t match the command that you actually entered, according to your post. Just for giggles, try running PowerShell.exe with the -NoProfile switch, then run Get-Command again and see what happens.

Dave, thanks, I tried this (details below) but still had the issue. Any other ideas? I’d like to reset my PowerShell environment in case there is something like a profile issue, but don’t know of any way to do this. Simply uninstalling and reinstalling WMF4 did not fix it (and when WMF4 was uninstalled PS3 still had a similar issue running get-command). I feel like I’m missing something, but don’t know what.

C:>powershell -noprofile
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:> [Environment]::GetCommandLineArgs()
powershell
-noprofile
PS C:> get-command
get-command : Object reference not set to an instance of an object.
At line:1 char:1

  • get-command
  •   + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceEx
     ception
      + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShe
     ll.Commands.GetCommandCommand
    
    

PS C:>

found an old thread with someone experiencing a similar issue.

https://powershell.org/forums/topic/show-command-error-ise/

Can’t help but think it is a profile issue. Do you have the same problem if you run Get-Command from the console? Also, try running powershell.exe -noprofile from the run dialog rather than from a commandline/ISE.

After you get the error, please run the following commands to dump more detailed information about it, and post the results here. Based on what I see so far, I’m still not sure what’s going on.

$error[0] | Format-List * -Force
$error[0].Exception | Format-List * -Force

Thanks Peter and Dave, here’s the output (from a -noprofile session, Run “powershell.exe -noprofile”)

PS C:\Users\John> get-command
get-command : Object reference not set to an instance of an object.
At line:1 char:1

  • get-command
  •   + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceEx
     ception
      + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShe
     ll.Commands.GetCommandCommand
    
    

PS C:\Users\John> $error[0] | Format-List * -Force

PSMessageDetails :
Exception : System.NullReferenceException: Object reference not
set to an instance of an object.
at Microsoft.PowerShell.Commands.GetCommandCommand.I
sCommandMatch(CommandInfo& current)
at Microsoft.PowerShell.Commands.GetCommandCommand.A
ccumulateMatchingCommands(IEnumerable`1 commandNames)
at System.Management.Automation.CommandProcessor.Pro
cessRecord()
TargetObject :
CategoryInfo : NotSpecified: (:slight_smile: [Get-Command], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Comm
ands.GetCommandCommand
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

PS C:\Users\John> $error[0].Exception | Format-List * -Force

Message : Object reference not set to an instance of an object.
Data : {}
InnerException :
TargetSite : Boolean
IsCommandMatch(System.Management.Automation.CommandInfo ByRef)
StackTrace : at Microsoft.PowerShell.Commands.GetCommandCommand.IsComman
dMatch(CommandInfo& current)
at Microsoft.PowerShell.Commands.GetCommandCommand.Accumula
teMatchingCommands(IEnumerable`1 commandNames)
at
System.Management.Automation.CommandProcessor.ProcessRecord()
HelpLink :
Source : System.Management.Automation
HResult : -2147467261

PS C:\Users\John> [Environment]::GetCommandLineArgs()
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
-noprofile
PS C:\Users\John> $psversiontable

Name Value


PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18444
BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2

PS C:\Users\John>

So weird… it looks like there may be a bug in Get-Command, but the problem is figuring out what’s triggering it. In the other post that Peter found, it was related to the Quest AD cmdlets in some way. It could be that one of the snapins or modules on your system is behaving the same way.

What do you get when you run these commands?

Get-Module -ListAvailable
Get-PSSnapin
Get-PSSnapin -Registered

John,

Found an Microsoft KB article related to the Get-Command error. This one is about PowerShell v2 and SQL Server 2008/2008 R2. Do you have SQL Server 2008 components installed on your Win7 machine?

http://support.microsoft.com/kb/2216478

I think it is worth checking this out and upgrade your SQL Server 2008 components if installed to at least the Service Pack specified in the article.

Please let us know your progress and run Dave’s commands as well to gather more details.

Best,
Daniel

Dave, thanks, output from those three gets is below

PS C:\Users\John> Get-Module -ListAvailable

Directory: C:\Users\John\Documents\WindowsPowerShell\Modules

ModuleType Version Name ExportedCommands


Script 0.0 adoLib {New-Connection, new-sqlcommand, invoke-sql, invoke-query…}
Script 1.0 Agent {Get-SqlConnection, Get-SqlServer, Get-AgentJobServer, Get…
Script 0.0 ISECreamBasic {Add-IseMenu, Remove-IseMenu}
Manifest 1.0.0.0 LogHelper {Get-LogFile, Get-LoggingPreference, Set-LogFile, Set-Logg…
Script 0.0 mySQLLib {New-MySQLConnection, new-MySqlCommand, invoke-MySql, invo…
Manifest 1.0 NetShell {ConvertFrom-HexIP, ConvertTo-BinaryIP, ConvertTo-Byte, Co…
Manifest 1.0.1.0 NetworkScan {Find-IPv4Device, Find-SqlServerService}
Script 0.0 OracleClient {new-oracle_connection, invoke-oracle_query, ConvertTo-ora…
Script 0.0 OracleIse {Connect-Oracle, Disconnect-Oracle, Prompt, Get-FileName…}
Script 0.0 PBM {Get-PolicyStore, Get-TargetServer, Write-PolicyEvalError,…
Script 0.0 PerfCounters {Invoke-Sqlcmd2, Get-ProcessPerfcounter, Write-LogText, Ge…
Script 0.0 RDS-Manager {Connect-RDSession, Disconnect-RDSession, Get-RDProcess, G…
Script 1.0 Repl {Get-SqlConnection, Get-ReplServer, Get-ReplLightPublicati…
Script 1.0 ShowMbrs {New-ShowMbrs, Set-ShowMbrs, Get-GroupUser, Get-ShowMbrs}
Script 0.0 SQLIse {Test-SqlScript, Out-SqlScript, Invoke-ParseSql, Format-Sq…
Script 1.0 SQLMaint {Invoke-DBMaint, Get-SqlConnection, Get-SqlServer, Get-Sql…
Binary 1.0 SQLParser {Test-SqlScript, Out-SqlScript}
Script 0.0 SQLProfiler {Invoke-Sqlcmd2, Save-InfoToSQLTable, Get-SQLProfiler}
Script 0.0 SQLPSX
Script 1.0 SQLServer {Get-SqlConnection, Get-SqlServer, Get-SqlDatabase, Get-Sq…
Manifest 1.0.1.0 SqlServerDatabaseEngineInformation Get-SqlServerDatabaseEngineInformation
Manifest 1.0.1.0 SqlServerInventory {Get-SqlServerInventory, Export-SqlServerInventoryToExcel,…
Script 1.0 SSIS {New-ISApplication, Copy-ISItemSQLToSQL, Copy-ISItemSQLToF…
Manifest 1.0.0.0 WindowsInventory {Get-WindowsInventory, Export-WindowsInventoryToExcel}
Manifest 1.0.1.0 WindowsMachineInformation Get-WindowsMachineInformation
Script 1.0 WPK {Add-CodeGenerationRule, ConvertFrom-TypeToScriptCmdlet, C…

Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version Name ExportedCommands


Manifest 1.0 xActiveDirectory
Manifest 1.2 xComputerManagement
Manifest 1.0 xDatabase
Manifest 1.0 xDscDiagnostics {Trace-xDscOperation, Get-xDscOperation}
Script 1.1 xDSCResourceDesigner {New-xDscResourceProperty, New-xDscResource, Update-xDscRe…
Manifest 1.1 xFailOverCluster
Manifest 2.1 xHyper-V
Manifest 2.1 xNetworking
Script 2.0.0.0 xPSDesiredStateConfiguration {New-PSWSEndpoint, Set-AppSettingsInWebconfig}
Manifest 1.0 xRemoteDesktopSessionHost
Manifest 1.0 xSmbShare
Manifest 1.1.1 xSqlPs
Manifest 1.0 xSystemSecurity
Manifest 1.2 xWebAdministration

Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules

ModuleType Version Name ExportedCommands


Manifest 1.0.0.0 AppLocker {Set-AppLockerPolicy, Get-AppLockerPolicy, Test-AppLockerP…
Manifest 1.0.0.0 BitsTransfer {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer,…
Manifest 1.0.0.0 CimCmdlets {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance,…
Script 1.0.0.0 ISE {New-IseSnippet, Import-IseSnippet, Get-IseSnippet}
Manifest 3.0.0.0 Microsoft.PowerShell.Diagnostics {Get-WinEvent, Get-Counter, Import-Counter, Export-Counter…
Manifest 3.0.0.0 Microsoft.PowerShell.Host {Start-Transcript, Stop-Transcript}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path…
Manifest 3.0.0.0 Microsoft.PowerShell.Security {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential…}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide…}
Manifest 3.0.0.0 Microsoft.WSMan.Management {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCredS…
Binary 1.0 PSDesiredStateConfiguration {Set-DscLocalConfigurationManager, Start-DscConfiguration,…
Script 1.0.0.0 PSDiagnostics {Disable-PSTrace, Disable-PSWSManCombinedTrace, Disable-WS…
Binary 1.1.0.0 PSScheduledJob {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-Jo…
Manifest 2.0.0.0 PSWorkflow {New-PSWorkflowExecutionOption, New-PSWorkflowSession, nwsn}
Manifest 1.0.0.0 PSWorkflowUtility Invoke-AsWorkflow
Manifest 1.0.0.0 TroubleshootingPack {Get-TroubleshootingPack, Invoke-TroubleshootingPack}

PS C:\Users\John> Get-PSSnapin

Name : Microsoft.PowerShell.Core
PSVersion : 4.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

PS C:\Users\John> Get-PSSnapin -Registered

Name : SqlServerCmdletSnapin100
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name : SqlServerProviderSnapin100
PSVersion : 2.0
Description : SQL Server Provider

PS C:\Users\John>

Daniel, thanks, I do have SQL 2008 R2 (and SQL 2012) instances on this machine. The SQL 2008 R2 instance is patched to Service Pack 1 (version 10.50.2550, SP1 is 10.50.2500) which is remedy suggested by the article.

Hmm. Please attempt to uninstall the SQL 2008 PowerShell Snap-in, if required for your work make sure you’ve the new SqlPs module of SQL 2012 installed and try again to run Get-Command.

Thanks
Daniel

Daniel, thanks, this is my first attempt at removing a PowerShell snap-in, and I’m not having any success. The snap-in shows in the “get” command but is ignored by the remove command. Am I missing a prefix?

PS Env:&gt; Get-PSSnapin -Registered

Name : SqlServerCmdletSnapin100
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name : SqlServerProviderSnapin100
PSVersion : 2.0
Description : SQL Server Provider

PS Env:&gt; remove-pssnapin -name SqlServerCmdletSnapin100
remove-pssnapin : No Windows PowerShell snap-ins matching the pattern ‘SqlServerCmdletSnapin100’ were found. Check the
pattern and then try the command again.
At line:1 char:1

  • remove-pssnapin -name SqlServerCmdletSnapin100
  •   + CategoryInfo          : InvalidArgument: (SqlServerCmdletSnapin100:String) [Remove-PSSnapin], PSArgumentExceptio
     n
      + FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.RemovePSSnapinCommand
    
    

PS Env:&gt; Get-PSSnapin -Registered | remove-pssnapin -whatif
remove-pssnapin : No Windows PowerShell snap-ins matching the pattern ‘SqlServerCmdletSnapin100’ were found. Check the
pattern and then try the command again.
At line:1 char:28

  • Get-PSSnapin -Registered | remove-pssnapin -whatif
  •                        ~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (SqlServerCmdletSnapin100:String) [Remove-PSSnapin], PSArgumentExceptio
      n
    • FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.RemovePSSnapinCommand

remove-pssnapin : No Windows PowerShell snap-ins matching the pattern ‘SqlServerProviderSnapin100’ were found. Check
the pattern and then try the command again.
At line:1 char:28

  • Get-PSSnapin -Registered | remove-pssnapin -whatif
  •                        ~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (SqlServerProviderSnapin100:String) [Remove-PSSnapin], PSArgumentExcept
      ion
    • FullyQualifiedErrorId : NoPSSnapInsFound,Microsoft.PowerShell.Commands.RemovePSSnapinCommand

PS Env:&gt;

This isn’t something I’ve done before either, but it looks like the list of registered Snapins is stored in the registry (under HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns\ ), and there are apparently no PowerShell cmdlets for registering or unregistering them. I’d recommend exporting a backup of that key first, then deleting one or both of the SQL snapin keys from that location.

Dave, thanks, those keys were there. After deleting them and restarting the machine this (below) is what happens. Any other ideas?

PS C:\Users\John> get-pssnapin

Name : Microsoft.PowerShell.Core
PSVersion : 4.0
Description : This Windows PowerShell snap-in contains cmdlets used to manage components of Windows PowerShell.

PS C:\Users\John> get-pssnapin -registered
PS C:\Users\John> get-command
get-command : Object reference not set to an instance of an object.
At line:1 char:1

  • get-command
  •   + CategoryInfo          : NotSpecified: (:) [Get-Command], NullReferenceException
      + FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Commands.GetCommandCommand
    
    

PS C:\Users\John>