I am a beginner learning the PowerShell environment.
I am wanting to create a report of network printers by selecting the Drivername.
Is there a way to remove (get rid of) in the output of the Drivername characters SeriesPCL?
Here is the code below, I have removed the server name.
Get-WmiObject -class win32_printer -ComputerName ServerName | Select Name, Drivername, Status | Where Drivername -match “Konica”
This is how it currently outputs:
Drivername
KONICA MINOLTA C3851SeriesPCL
KONICA MINOLTA C658SeriesPCL
KONICA MINOLTA C658SeriesPCL
KONICA MINOLTA C658SeriesPCL
KONICA MINOLTA C658SeriesPCL
KONICA MINOLTA C368SeriesPCL
I want it to look like this:
Drivername
KONICA MINOLTA C3851
KONICA MINOLTA C658
KONICA MINOLTA C658
KONICA MINOLTA C658
KONICA MINOLTA C658
KONICA MINOLTA C368
Olaf
October 20, 2022, 7:43am
2
nbusetti,
Welcome to the forum.
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Thanks in advance
How to format code in PowerShell.org <---- Click
You should not use Get-WmiObject
anymore. Instead use
There are several ways to manipulate strings in PowerShell. You could use the .SubString()
method of the type [STRING]
…
… like this:
$String = 'KONICA MINOLTA C658SeriesPCL'
$String.Substring(0,($String.Length - 9))
… or use the index of the type [ARRAY]
like this:
$String = 'KONICA MINOLTA C658SeriesPCL'
$String[0..($String.Length - 10)] -join ''
I’d probably go with the -replace
operator …
… like this:
Get-CimInstance -ClassName Win32_Printer -ComputerName SergverName |
Where-Object -Property DriverName -Match -Value 'Konica' |
Select-Object -Property Name, Drivername, Status,
@{
Name = 'FirendlyName'
Expression = { $_.DriverName -replace 'SeriesPCL$' }
}
2 Likes
Hi Olaf,
Thanks for replying to my question and advising the best way to post code I will check out the link.
I have tried the code suggestion you constructed and it works great
It is great to learn to use Get-CimInstance instead of GetWmiObject, there are so many websites on PowerShell and so much to learn about it.