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
    } 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

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}
Sage
  • 33
  • 4
0

Inside ForEach, specify the shareName property like this.

ForEach ($shareName in $text) { 

 $shareName.sharename

}
hysh_00
  • 819
  • 6
  • 12
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*"}}
Booga Roo
  • 1,665
  • 1
  • 21
  • 30