Skip to content

Clear-Variable.md Documentation Section INPUTS is wrong - allows PSVariable object as input variable in pipe - tested on PS Core 7.5.5 #13056

@groeskens

Description

@groeskens

Prerequisites

  • Existing Issue: Search the existing issues for this repository. If there is an issue that fits your needs do not file a new one. Subscribe, react, or comment on that issue instead.
  • Descriptive Title: Write the title for this issue as a short synopsis. If possible, provide context. For example, "Typo in Get-Foo cmdlet" instead of "Typo."
  • Verify Version: If there is a mismatch between documentation and the behavior on your system, ensure that the version you are using is the same as the documentation. Check this box if they match or the issue you are reporting is not version specific.

Links

Summary

The section ## INPUTS. in reference/7.6/Microsoft.PowerShell.Utility/Clear-Variable.md is wrong or outdated.
It states:

## INPUTS

### None

You can't pipe objects to this cmdlet.

At least since Powershell Version 7.5.5 it's possible to pipe a variable of type System.Management.Automation.PSVariable to the Cmdlet Clear-Variable. The same is true and documented correctly for the Cmdlet Remove-Variable

Details

Short Test Script

This short test shows, that we can pipe a System.Management.Automation.PSVariable to the Cmdlet Clear-Variable

### Running on Powershell Core Edition 7.5.5 (Windows 11 64-bit x64 AMD64 10.0.26200) 
$PSVersionTable | Select-Object -Property PSVersion, PSEdition | Format-List

# PSVersion : 7.5.5
# PSEdition : Core

# set string variable
$TestDemoStringVariable = "This is a test string"

# clear the variable with pipe works
Get-Variable TestDemoStringVariable | Clear-Variable

# variable is no cleared to $null
$TestDemoStringVariable -eq $null
### Output ### True

# set variable and assign as 

$TestDemoStringVariable = "This is a test string"
$TestDemoStringVariableObject = Get-Variable TestDemoStringVariable
$TestDemoStringVariableObject.GetType().FullName
### Output ### System.Management.Automation.PSVariable

# clear the variable by pipe
$TestDemoStringVariableObject | Clear-Variable
$TestDemoStringVariableObject
### Output ###Name                           Value
### Output ###----                           -----
### Output ###TestDemoStringVariable

# check variable is cleared ($null)
$TestDemoStringVariable -eq $null
### Output ### True

Full Test List

$PSVersionTable.PSVersion.ToString()
### Output ### 7.5.5
$TestDemoStringVariable = "This is a test string"
$TestDemoStringVariable
### Output ### This is a test string
$TestDemoStringVariable.GetType().Name
### Output ### String
(Get-Variable TestDemoStringVariable).GetType().Name
### Output ### PSVariable
Get-Variable TestDemoStringVariable | Clear-Variable
$TestDemoStringVariable
$TestDemoStringVariable.GetType().Name
### Output ### InvalidOperation: You cannot call a method on a null-valued expression.
$TestDemoStringVariable.GetType()
InvalidOperation: You cannot call a method on a null-valued expression.
$TestDemoStringVariable -eq $null
### Output ### True
$TestDemoStringVariable = "This is a test string"
$TestDemoStringVariable
### Output ### This is a test string
$TestDemoStringVariable.GetType().Name
### Output ### String
[PSVariable]"TestDemoStringVariable" | Clear-Variable
$TestDemoStringVariable -eq $null
### Output ### True
$TestDemoStringVariable
[string] $TestDemoStringTypedVariable = "This is a typed string variable"
Get-Variable "TestDemoStringTypedVariable" | Clear-Variable
$TestDemoStringTypedVariable.GetType().Fullname
### Output ### System.String
($TestDemoStringTypedVariable -eq $null)
### Output ### False
$TestDemoStringTypedVariable.Value
($TestDemoStringTypedVariable.Value -eq $null)
### Output ### True
$TestDemoStringVariable = "This is a test string"
$TestDemoStringVariableObject = Get-Variable TestDemoStringVariable
$TestDemoStringVariableObject.GetType().FullName
### Output ### System.Management.Automation.PSVariable
$TestDemoStringVariableObject
### Output ### Name                           Value
### Output ### ----                           -----
### Output ### TestDemoStringVariable         This is a test string

$TestDemoStringVariableObject | Clear-Variable
$TestDemoStringVariableObject
### Output ###Name                           Value
### Output ###----                           -----
### Output ###TestDemoStringVariable

$TestDemoStringVariable -eq $null
### Output ### True

Suggested Fix

Change Section ## INPUTS. in reference/7.6/Microsoft.PowerShell.Utility/Clear-Variable.md as piping a System.Management.Automation.PSVariable object is allowed. The old Information None, "You can't pipe objects to this cmdlet." is wrong.

New suggested text for the section ## INPUTS. starting on line 220 in version 7.6:

## INPUTS

### System.Management.Automation.PSVariable

You can pipe a variable object to this cmdlet.

This is the same text used for the section in reference/7.6/Microsoft.PowerShell.Utility/Remove-Variable.md

Metadata

Metadata

Assignees

Labels

issue-doc-bugIssue - error in documentation

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions