0

I am trying to remove part of the text that is returned from get-wmiobject, the text I am trying to remove is @{ShareName=PRT47} and I only want the printer name that is returned so the suffix if you will.

I have tried different by attempting to get the number of characters and delete them but I have since found out that you can also use what is in the proceeding cmdlet.

Remove Text

   $text  =  Get-WmiObject -Class $class -Filter Network=True | Select ShareName | Where-Object {$_ -   like "*PRT*"} 
    ForEach ($shareName in $text) { 
    $newprinter = "\\$newserver\$shares"
    rundll32 printui.dll,PrintUIEntry /in /n $newprinter
    } 

3 Answers 3

1

You should be able to use -replace to find and replace strings with blank entries. Using your example of @{ShareName=PRT47}

$text = $text -replace '(.*)=(.*)' ${2}
echo $text //Prints PRT47}

An alternative is to use split. The below example will split on each equal sign.

$text = $text -split '='
echo $text[0] //prints @{ShareName
echo $text[1] //prints PRT47}
Sign up to request clarification or add additional context in comments.

Comments

0

Inside ForEach, specify the shareName property like this.

ForEach ($shareName in $text) { 

 $shareName.sharename

}

Comments

0

Here's another way to do it if you have Powershell 3.0 or higher:

$text  =  (Get-WmiObject -Class $class -Filter Network=True).ShareName | Where-Object {$_ -like "*PRT*"}

Or, closer to Hysh_00's suggestion if you only have Powershell 2.0:

$text  =  Get-WmiObject -Class $class -Filter Network=True | foreach {$_.ShareName | Where-Object {$_ -like "*PRT*"}}

Comments

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.