public static T GetVariableValue <T>(this PSCmdlet cmdlet, string name) { var psVariable = cmdlet.Invoke <PSVariable>($"Get-Variable|where{{$_.Name -eq '{name}'}}").FirstOrDefault(); if (psVariable == null) { throw new NullReferenceException(name); } if (typeof(T).IsEnum) { return((T)Enum.Parse(typeof(T), psVariable.Value.ToString(), true)); } return((T)psVariable.Value); }