powershellprinters

Get-Printer Powershell script still showing columns even though they are excluded


Have the following line which should just output the name port and driver using get-printer: Get-Printer | Select-Object Name, PortName, DriverName

The problem is that it also throws in Status, PSComputerName, and RunspaceId columns as shown, which it shouldn't. I tried adding the Excludeproperty which stops the Status column, but not the other two Get-Printer | Select-Object Name, PortName, DriverName -ExcludeProperty Status, PSComputerName, RunspaceId

At a loss, that the heck is going on. Full script for reference.

# Define the path to the text file
$filePath = "C:\powershell\complist.txt"

# Check if the file exists
if (Test-Path $filePath) {
    # Read the computer names from the file
    $computerNames = Get-Content $filePath

    # Loop through each computer name
    foreach ($computer in $computerNames) {
        try {
            # Use Invoke-Command to run Get-Printer on the remote computer
            $printerInfo = Invoke-Command -ComputerName $computer -ScriptBlock {
                Get-Printer | Select-Object Name, PortName, DriverName -ExcludeProperty Status, PSComputerName, RunspaceId
            }

            if ($printerInfo) {
                # Output the printer information
                Write-Host "Printers on $computer :"
                $printerInfo | Format-Table -AutoSize
            } else {
                Write-Host "No printers found on $computer."
            }
        } catch {
            Write-Host "Error retrieving printers from '$computer': $_"
        }
    }
} else {
    Write-Host "File not found: $filePath"
}

Ouput information from get-printer command and only getting the Name, PortName, and DriverName columns


Solution

  • Instead of using Select-Object within the remote session (ScriptBlock), we retrieve all the printer data first. Then, after retrieving the data, we apply Select-Object on the local machine to remove the extra properties (PSComputerName, RunspaceId) added by the remote session.

    # Define the path to the text file
    $filePath = "C:\powershell\complist.txt"
    
    # Check if the file exists
    if (Test-Path $filePath) {
        # Read the computer names from the file
        $computerNames = Get-Content $filePath
    
        # Loop through each computer name
        foreach ($computer in $computerNames) {
            try {
                # Use Invoke-Command to run Get-Printer on the remote computer
                $printerInfo = Invoke-Command -ComputerName $computer -ScriptBlock {
                    Get-Printer
                }
    
                if ($printerInfo) {
                    # Output the filtered printer information locally
                    Write-Host "Printers on $computer :"
                    $printerInfo | Select-Object Name, PortName, DriverName | Format-Table -AutoSize
                } else {
                    Write-Host "No printers found on $computer."
                }
            } catch {
                Write-Host "Error retrieving printers from '$computer': $_"
            }
        }
    } else {
        Write-Host "File not found: $filePath"
    }