PowerShell Repair?

This topic contains 23 replies, has 7 voices, and was last updated by  Andy Peaple 7 months, 1 week ago.

  • Author
    Posts
  • #15343

    John Drake
    Participant

    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

  • #15352

    Richard Siddaway
    Moderator

    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.

  • #15353

    John Drake
    Participant

    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).

  • #15354

    Richard Siddaway
    Moderator

    OK – you can uninstall/reinstall on that platform.

  • #15356

    John Drake
    Participant

    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

  • #15358

    John Drake
    Participant

    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

  • #15360

    Dave Wyatt
    Moderator

    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.

  • #15388

    John Drake
    Participant

    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:\>

  • #15398

    Peter Jurgens
    Participant

    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.

  • #15399

    Dave Wyatt
    Moderator

    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
    
  • #15407

    John Drake
    Participant

    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: (:) [Get-Command], NullReferenceException
    FullyQualifiedErrorId : System.NullReferenceException,Microsoft.PowerShell.Comm
    ands.GetCommandCommand
    ErrorDetails :
    InvocationInfo : System.Management.Automation.InvocationInfo
    ScriptStackTrace : at , : 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>

  • #15418

    Dave Wyatt
    Moderator

    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
    
  • #15432

    Daniel Krebs
    Moderator

    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

  • #15435

    John Drake
    Participant

    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>

  • #15436

    John Drake
    Participant

    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.

  • #15437

    Daniel Krebs
    Moderator

    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

  • #15444

    John Drake
    Participant

    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:\> 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:\> 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:\> 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:\>

  • #15445

    Dave Wyatt
    Moderator

    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.

  • #15492

    John Drake
    Participant

    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>

  • #15494

    Dave Wyatt
    Moderator

    Bummer, I was hoping it would be one of those snapins, based on earlier hints of SQL causing the same problem for other people.

    Next, I'd try the modules. A quick way of seeing if this will be helpful is to rename your My Documents\WindowsPowerShell\Modules folder, launch PowerShell, and try Get-Command again. If it works, then you can try creating a new Modules folder and moving them in a few at a time. Hopefully, you'll be able to identify one or more modules that are triggering this problem with Get-Command.

  • #15495

    John Drake
    Participant

    Brilliant, Dave, renaming that modules directory (and I created a fresh empty one) has fixed both get-command and get-dscresource (which was where I started).

    I've put in a Connect suggestion (ID 878376) for PowerShell Repair as it seems to me it would be good if there was a simple way to say "take me back to a clean working default installation of PowerShell (and please discard profiles, snap-ins, modules, etc)".

    Many thanks for all the help.

    PS Env:\> get-command

    CommandType Name ModuleName
    ———– —- ———-
    Function A:
    Function B:
    Function C:
    Function cd..
    Function cd\
    ...

    PS Env:\> get-dscresource

    ImplementedAs Name Module Properties
    ————- —- —— ———-
    Binary File {DestinationPath, Attributes, Checksum, Con...
    PowerShell Archive PSDesiredStateConfiguration {Destination, Path, Checksum, DependsOn...}
    PowerShell Environment PSDesiredStateConfiguration {Name, DependsOn, Ensure, Path...}
    PowerShell Group PSDesiredStateConfiguration {GroupName, Credential, DependsOn, Descript...
    Binary Log PSDesiredStateConfiguration {Message, DependsOn}
    PowerShell Package PSDesiredStateConfiguration {Name, Path, ProductId, Arguments...}
    PowerShell Registry PSDesiredStateConfiguration {Key, ValueName, DependsOn, Ensure...}
    PowerShell Script PSDesiredStateConfiguration {GetScript, SetScript, TestScript, Credenti...
    PowerShell Service PSDesiredStateConfiguration {Name, BuiltInAccount, Credential, DependsO...
    PowerShell User PSDesiredStateConfiguration {UserName, DependsOn, Description, Disabled...
    PowerShell WindowsFeature PSDesiredStateConfiguration {Name, Credential, DependsOn, Ensure...}
    PowerShell WindowsProcess PSDesiredStateConfiguration {Arguments, Path, Credential, DependsOn...}
    PowerShell xADDomain xActiveDirectory {DomainAdministratorCredential, DomainName,...
    ...

  • #21434

    ps_gregg
    Participant

    I just discovered that I had the same problem with Get-Command. I didn't realize I had the issue because I rarely run Get-Command without parameters. With the help of this article I tracked it down to the Module "RDS-Manager". Once I removed the module from my modules directory(C:\Program Files\WindowsPowerShell\Modules), Get-Command started working again.

  • #61563

    Andy Peaple
    Participant

    I had the same problem myself. It was caused by setting StrictMode inside of a module. Once I removed the line, I no longer received the error which incidentally was triggered inside of the PSDesiredStateConfiguration module.

  • #15349

    Daniel Krebs
    Moderator

    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).

You must be logged in to reply to this topic.