Powershell printer script - powershell

I will translate my question in english so that everyone can read it!
Am not really good in powershell i work on it for 3 days.
I need to create a powershell script to show what is the default printer selected in the computer ( example :PrinterA) before the execute a line to select another printer as default printer ( PrinterB) .
After that i need to reset the old default printer ( PrinterA).
I execute line:
Get-WmiObject -query " SELECT * FROM Win32_Printer WHERE Default=$true"
to show default printer but i dont know how to memorize it.
To select the (printeB ), I do:
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "PrinteB"
Can you help me please ?

I'd use
$OldDefaultPrinter = (Get-WmiObject win32_printer | Where-Object Default -eq $True).Name
To store the current default printer in a variable.
To restore with your method
RUNDLL32 PRINTUI.DLL,PrintUIEntry /y /n "$OldDefaultPrinter"

you did not mention the specific version of Powershell that you are using. [grin] presuming you are running ps5.1 on win10, you can use the print management cmdlets to do what you need. take a look at this ...
PrintManagement
— https://learn.microsoft.com/en-us/powershell/module/printmanagement/?view=win10-ps
the Get-Printer cmdlet will get info about the available printers. the Set-Printer cmdlet will let you set the default printer.

Related

How do I change the hostname of a Windows PC with batch or powershell

I want to change the hostname of a computer from Batch (a.k.a. Command Prompt) or Powershell.
Initially I started research into using the wmic command. But running wmic /? on Windows 10 21H1 indicates it is now deprecated.
Then I looked at Get-WmiObject. But when I run man Get-WmiObject in PowerShell, the description indicates it has been "superseded" by Get-CimInstance.
Using the old Get-WmiObject command you could change your own computer's hostname with (Get-WmiObject Win32_ComputerSystem).Rename("New-Hostname").
What is a non-deprecated way to change your own Windows computer's hostname using Batch or PowerShell?
Thanks to #Theo for the tip.
The PowerShell command Rename-Computer
Rename-Computer "new-hostname"
Admin privileges and a computer restart are required.
The command warns you if the length of the hostname is longer than 15 characters.
The batch-file command...
NETDOM RENAMECOMPUTER "%ComputerName%" /Newname:"NewNameGoesHere" /FORCE
I made a simple code if you want to use it
#echo off
set /p newname=The name of the new device:
wmic computersystem where name="%computername%" call rename name="%newname%"
Anyway, this is what you are looking for
wmic computersystem where name="%computername%" call rename name="newname"

how to use wmi with powershell to target a specific printer for output

I have an amazing script i am going to use to for a bespoke print solution. There is one thing left to do. The script utilizes Start-Process –FilePath “c:\testfolder\*.docx” –Verb Print
how can i make it so it targets an installed printer on the client system without targeting/using the default printer? (another VBScript already uses the default).
PSVersion5. + W764bit
(New-Object -ComObject WScript.Network).SetDefaultPrinter('Xerox Floor X')
This does the trick, it will select the relevant default printer and then i can run another line which will default it back to the original default printer after the script runs.
Some issues with parentheses running and defaulting it back before it prints at the moment though.
I dont think you can specify the printer when using the Print verb. What you can do is this:
Get-ChildItem -Path "C:\testfolder" -Filter *.docx | Out-Printer "\\Server01\Prt-6B Color"
Get all items in path with extension .docx pass to Out-Printer
More on Out-Printer

command on specific device

I am trying to write a batch script, which will always be executed at a specific location (in this case on my USB-Stick), so typically I would use D:, but sometimes the stick has another drive letter. Therefore I am trying to find the device via its name (USB_Stick).
I haven't found a way to do this via a batch command.
A PowerShell command would look like this:
#(get-wmiobject -query \"select deviceid from win32_logicaldisk where volumename='USB-STICK'\")[0].deviceid"
but I don't know how to use the result of this PowerShell command.
I tried things like this:
for /f "usebackq" %%x in (`powershell.exe -Command "#(get-wmiobject -query \"select deviceid from win32_logicaldisk where volumename='USB-STICK'\")[0].deviceid"`) do (
set res=%%x
)
#echo %res%
but the result of this would only be ommands.GetWmiObjectCommand and not the D:.
If you're going for a batch-script anyway, use the wmic commandline utility:
#echo off
for /f "delims== tokens=2" %%d in (
'wmic logicaldisk where volumename^="USB-STICK" get deviceid /value'
) do set "deviceId=%%~d"
echo %deviceId%
Using the wmic with the /value parameter creates name=value lines as the output, which you can split in the for loop by defining = as the delimiter.
If I understand correctly, the batch file is running on the USB stick? If so, then the drive letter of the stick (without a slash) can be obtained via %~d0

Openfiles query to see open files

How can I call the Openfiles.exe (which is on the server 2008 file server) remotely from a computer to see which files are open by the users? I also need to have it login as the domain admin user in the parameters.
Openfiles can do a server direct. You don't have to run it remotely.
From Help openfiles /query /?
OPENFILES /Query /S system /U username /P password /NH
A way to do it in PowerShell and to allow for searching would be with this small function.
function Get-OpenFiles {
cls
openfiles /query /s $args[0] /fo csv /V | Out-File -Force C:\temp\openfiles.csv
$search = $args[1]
Import-CSV C:\temp\openfiles.csv | Select "Accessed By", "Open Mode", "Open File (Path\executable)" | Where-Object {$_."Open File (Path\executable)" -match $search} | format-table -auto
Remove-Item C:\temp\openfiles.csv
}
It allows you to call Get-OpenFiles Server Filename and it will show you the results. The caveat is that you have a folder called C:\temp.
So if I do Get-OpenFiles TestServer Hardware I get the below.
Accessed By Open Mode Open File (Path\executable)
----------- --------- ---------------------------
NFDJWILL Read N:\Network Services\Documentation\Hardware.xlsx
NFDJWILL Write + Read N:\Network Services\Documentation\Hardware.xlsx
NFDJWILL Read N:\Network Services\Documentation\Hardware.xlsx
Here is a solution without creating temporary files.
function Get-OpenFile
{
Param
(
[string]$ComputerName
)
$openfiles = openfiles.exe /query /s $computerName /fo csv /V
$openfiles | ForEach-Object {
$line = $_
if ($line -match '","')
{
$line
}
} | ConvertFrom-Csv
}
Get-OpenFile -ComputerName server1
You can now do this with the PowerShell command Get-SmbOpenFile.
how to run application remotely by using Power Shell
https://technet.microsoft.com/en-us/library/dd819505.aspx
your PS or BAT script will be:
openfiles.exe > c:\temp\openfiles.txt
of course better use different output folder and file name.
Alternative way:
https://technet.microsoft.com/en-ca/sysinternals/bb897553.aspx -- PsExec -- PsExec is a light-weight telnet-replacement that lets you execute processes on other systems, complete with full interactivity for console applications, without having to manually install client software.
You can use a remote delegated powershell session for this.
Use Delegated Administration and Proxy Functions
You can delegate the admin credentials in RunAs setting of the session configuration, and constrain the session to only being able to run the openfiles.exe. Then you can assign permission to use to session to selected groups or users. This enables you to let people run cmdlets or programs that require domain admin authority, without giving them the domain admin credentials.
No need to create a CSV file and have to remove it later, or be concerned about its' size.
openfiles /s MyFileSrv /query /fo CSV /v /u admin1 /p myPass | convertfrom-csv |?{$_.'Open File (Path\executable)' -match "MyFolder"} |select 'Accessed By', 'Open Mode', 'Open File (Path\executable)
Accessed By Open Mode Open File (Path\executable)
----------- --------- ---------------------------
robinsonl Write + Read D:\Dept\MyFolder
smithd Read D:\Dept\MyFolder\info
If you're using powershell 1.0 then add |select -skip 8| before the convertfrom-csv portion.

Struggling with foreach

I'm rather new and, well, awful at this whole scripting thing: so any help would be appreciated.
Basically I am trying to create a PowerShell script that installs an undefined number of printers on an undefined number of computers. The computer names and printer names will come from local text files.
This is what I have so far:
$credentials = Get-Credential
$printerlist = Get-Content c:\setup\printers.txt
get-content c:\setup\names.txt | foreach-object {foreach($printer in $printerlist){rundll32 printui.dll PrintUIEntry /ge /c $_ /n $printer}}
EDIT: I am getting the error, unable to enumerate per machine printer connections operation could not be completed (error 0x0000007b) I have tried modifying the script anyway i can come up with, which is probably fewer ways than it should be.
I don't think you have an issue with your foreach loop here.
I think it's just the usage of rundll32 printui.dll PrintUIEntry
Install printer:
rundll32 printui.dll,PrintUIEntry /in /c "\\COMPUTER_NAME" /n "\\PRINT_SERVER_NAME\PRINTER_NAME"
Sets default printer:
rundll32 printui.dll,PrintUIEntry /y /c "\\COMPUTER_NAME" /n "\\PRINT_SERVER_NAME\PRINTER_NAME"
Try installing with the /in individually for one computer from powershell console without your script to see if you still get the same error, could be a permission but I don't think so.