I am new to PowerShell but I found I can use Substring to count to the right or left of a string within a variable. It appears though it is not supported for the output I am receiving. I am hoping someone can point me in the right direction. Thank you for any help.
Code to retrieve the computer name.
$compname = WmiObject -class Win32_ComputerSystem | Select-Object Name
$compname
$compname.Substring(9,0)
Here is the result and error:
Name
Computer-PC Method invocation failed because [Selected.System.Management.ManagementObject] does not contain a method named 'Substring'. At line:3 char:1
- $compname.Substring(9,0)
-
+ CategoryInfo : InvalidOperation: (Substring:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound


$compname = WmiObject -class Win32_ComputerSystem | ForEach-Object Nameor$compname = (WmiObject -class Win32_ComputerSystem).Name$compname = WmiObject -class Win32_ComputerSystem | Select-Object -ExpandProperty Nameshould give you a[System.String], and[System.String]has the method you're looking for. Also, check out the environment variable$env:COMPUTERNAME.