0

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
    
3
  • 1
    You don't need sub string, use $compname = WmiObject -class Win32_ComputerSystem | ForEach-Object Name or $compname = (WmiObject -class Win32_ComputerSystem).Name Commented Jun 9, 2022 at 18:07
  • 2
    $compname = WmiObject -class Win32_ComputerSystem | Select-Object -ExpandProperty Name should give you a [System.String], and [System.String] has the method you're looking for. Also, check out the environment variable $env:COMPUTERNAME. Commented Jun 9, 2022 at 18:07
  • Thank you both for your input. I think I will be going with Zett42, ForEach-Object. It works great. The output is clean how I need it. I looked a long time and never found anything like this. It will also help with some other code I have as well. Thanks a million. I just checked the other option and what notjustme gave and they all have the same results. Thanks again. Commented Jun 9, 2022 at 18:38

1 Answer 1

1

This error occurs because you're trying to use the Substring method on an object.

Take a look, if i do the same query that you did, it returns me an object with "Name" property:

enter image description here

And as the powershell error shows, you cannot call the substring method directly to an object. You must do it on a string, in this case, the property name. To solve you problem, you just need to call "Name" property in your query. Something like this:

$computerName = (Get-WmiObject Win32_ComputerSystem).Name

After that, you will be able to use "Substring" method because that query returns a string:

enter image description here

If any other problem occurs, i will be glad to help you :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Robert for this information, zett42 gave the same answer to use "$computerName = (Get-WmiObject Win32_ComputerSystem).Name" which I've accepted. Thank you for explaining that a Substring cannot be used on an object. I have a lot to learn about PowerShell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.