Niklas Sjögren
Niklas Sjögren

Reputation: 102

Powershell output single value

I have the following issue. I want to output my default printer name to a .txt-file for use in another script to set this printer as default for other computers. the query I use is:

Get-WmiObject -query "select * from win32_printer where default=$true"

it gives me this data:

Location      : xxxx
Name          : \\printserver\nameofprinter
PrinterState  : 0
PrinterStatus : 3
ShareName     : nameofprinter
SystemName    : \\printserver

But I want the query to give me only the value for "Name" when i Out-File it. so far i have used Format-List -Property Name. But this outputs the whole line. I only want the "\Printerserver\nameofprinter" in my outfile.

Upvotes: 0

Views: 994

Answers (2)

Fazer87
Fazer87

Reputation: 1242

Either:

Get-WmiObject -query "select * from win32_printer where default=$true" | select -ExpandProperty Name

OR

(Get-WmiObject -query "select * from win32_printer where default=$true").Name

Will do what you want.

Hope this helps

EDIT: As you only wanted to write a file IF a printer exists, try something like the following (tested with printer, but I'm not uninstalling all my printers to test without!)

if ( $printerName = (Get-WmiObject -query "select * from win32_printer where default=$true").Name ) { New-Item "C:\folder\printer.txt" -type file -Value $printerName -force }

Upvotes: 0

MonkeyDreamzzz
MonkeyDreamzzz

Reputation: 4368

Get-WmiObject -query "select * from win32_printer where default=$true" | select -ExpandProperty Name

Upvotes: 2

Related Questions