# Is there a way to get the variable name from class

**URL:** <https://forums.powershell.org/t/is-there-a-way-to-get-the-variable-name-from-class/25245>\
**Category:** PowerShell Help\
**Created:** [January 3, 2025, 5:22pm UTC](https://forums.powershell.org/t/is-there-a-way-to-get-the-variable-name-from-class/25245 "2025-01-03T17:22:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dengshen](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dengshen/32/5075_2.png) [@dengshen](https://forums.powershell.org/u/dengshen)\
**Post date:** [January 3, 2025, 5:22pm UTC](https://forums.powershell.org/t/is-there-a-way-to-get-the-variable-name-from-class/25245/1 "2025-01-03T17:22:57Z")

</div>

For a long time i try to simplize process of giving variable and save it with name of the variable and value ,but i didn’t seems to find a way to do so

```auto
using namespace System.Xml.Linq
BeforeAll{

    class ADvar {
        [string]$name
        [string]$value
        [string]$description
        [string]$path = "$psscriptroot\xml\advar.xml"

        [void] DoInit([pscustomobject]$pscustomobject){
            $pscustomobjectName = (($pscustomobject) | Get-Member -Type NoteProperty).Name
            foreach ($propertyName in $pscustomobjectName) {
                $this.$propertyName = $pscustomobject.$propertyName
            }
        }

        ADvar ([pscustomobject]$pscustomobject){
            $this.DoInit($pscustomobject)
        }

        [object] save(){
            $object = [XElement]::new("object",
            [XAttribute]::new("name","advar"),
            [XElement]::new("property",[XAttribute]::new("name","name"),$this.name),
            [XElement]::new("property",[XAttribute]::new("name","value"),$this.value),
            [XElement]::new("property",[XAttribute]::new("name","description"),$this.description)
            )
            return $object.ToString()
        }

        [string] GetVariableName([object]$variable) {
            $variableName = (Get-Variable | Where-Object { $_.Value -eq $variable }).Name
            return $variableName
        }
    }
     }

    Describe "it test ADvar" {
        context "test first"{
            it "test the getvariableName"{

                $advar = [ADvar]::new([pscustomobject]@{
                    name = "test"
                    value = "test"
                    description = "test"
                })
    
                $advar.GetVariableName($advar) | Should -Be "advar"
            }
        }
      
    }

```

```auto
powershell > test.advar.ps1

Starting discovery in 1 files.
Discovery found 1 tests in 10ms.
Running tests.
[-] it test ADvar.test first.test the getvariableName 15ms (14ms|1ms)
 Expected strings to be the same, but they were different.
 Expected length: 5
 Actual length: 58
 Strings differ at index 0.
 Expected: 'advar'
 But was: '? advar IsCoreCLR IsWindows isWindows10 this true variable'
            ^
 at $advar.GetVariableName($advar) | Should -Be "advar", C:\Users\Adminnistrator\my-modules\run\test.adclass.ps1:49
 at <ScriptBlock>, C:\Users\Adminnistrator\my-modules\run\test.adclass.ps1:49

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 2, 2025, 5:22pm UTC](https://forums.powershell.org/t/is-there-a-way-to-get-the-variable-name-from-class/25245/2 "2025-02-02T17:22:58Z")

</div>

Closed on February 2, 2025, 5:22pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
