I am trying to make a Powershell script that will run in the OU so when the user shuts down their laptop it will remove the local printer and when they turn on their laptop it will re-add and rename the locally attached printer (this will allow the laptops to be docked into different rooms and still use the local printers). The startup script works fine. it re-adds the printer, renames it and sets the trays etc. however the shutdown script doesn't seem to be working correctly.
When the shutdown script is run it removes the printer from device manager/print manager/registry but it still seems to be showing in devices and printer as "driver not available". So when the laptop is started back up the startup script doesn't work as the printer is sitting in error state for the above. a work around is right clicking the printer in devices and printers and pressing "Remove Device" but obviously can not do this during shutdown.
Is there anyway to remove a device from "Devices and Printers" via Powershell? it does work with network printers but just won't fully remove local printers.
$GetDriver = (Get-Printer -Name "Reports" | Select-Object DriverName | ConvertTo-Csv -NoTypeInformation -Delimiter ",") | % {$_ -replace '"',''} | Select-Object -Skip 1
$GetPort = (Get-Printer -Name "Reports" | Select-Object PortName | ConvertTo-Csv -NoTypeInformation -Delimiter ",") | % {$_ -replace '"',''} | Select-Object -Skip 1
Get-Printer -Name "Reports" | Rename-Printer -NewName $GetDriver
Remove-Printer -Name $GetDriver
Remove-PrinterPort -Name $GetPort
Thanks in advance for any help
Related
Asking for a bit of help with my script. I would like to get a list saved to a thumb drive with the computer name and the WAN address. I know I can add a custom label with the computer name but would get the default name. I already set the computer to change the name on reboot but I do not want to reboot as there are other things that need to happen before rebooting. The computers are new and are not added to the domain. I have a thumb drive that will auto-load and assist in changing the name without asking to reboot. So other software can install.
This is what I have so far:
getmac /v /FO CSV | ConvertFrom-Csv | Select-Object #{n='ComputerName';e={$env:COMPUTERNAME}},'Physical Address','Connection Name' | Where-Object { $_.'Connection Name' -Match 'Wi-Fi' }
And this is the output:
ComputerName Physical Address Connection Name
------------------ ------------------ --------------------
Desktop-9K293 XX-XX-XX-XX-XX Wi-Fi
I want the ComputerName to be the new name before rebooting. This way I can add an Export-CSV ~Location\MAC_Report.CSV -NoTypeInformation -append at the end of that code. Is there a way? Or do I need to restart the computer and generate that CSV using the same code?
I would love it to look:
ComputerName Physical Address Connection Name
------------------ ------------------ --------------------
NEW_____Name XX-XX-XX-XX-XX Wi-Fi
There are a lot of things I can make my thumb drive do. This way I have fewer restarts before adding the rest of the software(s) that also requires a restart. This way I cut my restart to one, on both all the software and computer rename.
I ended up just saving a TXT to the desktop of the computer, this way I can add the new computer name manually. This way I can then grab all the text files and consolidate them into one CSV or txt file. A bit of work but at least I can set the restart for later while I am able to install the rest of the software.
getmac /v /FO CSV | ConvertFrom-Csv | Select-Object 'Physical Address','Connection Name' | Where-Object { $_.'Connection Name' -Match 'Wi-Fi' } | Export-CSV -Path "$env:USERPROFILE\Desktop\WLAN_MAC.TXT" -NoTypeInformation
If there was a better way I would have done it.
We have a large effort underway for specific PC’s (approximately 10,000) that need to be renamed. They are in workgroup mode (not domain joined). Obviously if we can script this and do it remotely we should. I have been trying to better understand PowerShell and think it can actually be done pretty easily if I can get the code right. I need a very simple script that will:
Get the current IP address of the machine.
Compare that IP address to a CSV formatted list.
From the list, use the new Computer Name based on the IP Address and rename the computer.
The CSV would be very simple:
IPADDRESS,NEWCOMPNAME
192.168.0.1,NewPC1
192.168.0.2,NEWPC2
192.168.0.3,NEWPC3
This is the script I have so far but is not working:
$currentIpAddress = Test-Connection $env:COMPUTERNAME -count 1 | select Address, Ipv4Address
$csv = Import-Csv C:\test.csv
$newComputerName = $csv | where {$_.IPADDRESS -eq $currentIpAddress} | % NEWCOMPNAME
Rename-Computer -newname $newComputerName -Force -Restart
Thanks all for your comments and questions. I figured it out. Just to answer the questions and post the correct code for others, here goes. I am hitting Windows 8.1 x64 and Windows 10 x64. Powershell 4 and 5. If the computer name is not in the list, then the script fails (which is good) and does nothing. Also, we are running this as the local admin account, so the tests have proven successful so far.
The updated scripts are:
The CMD we are using:
If Not Exist C:\Temp MD C:\Temp
Copy /Y "%~dp0RenameComputerBasedOnIPList.csv" C:\temp\RenameComputerBasedOnIPList.csv
powershell -ExecutionPolicy ByPass -File "%~dp0RenameComputerBasedOnIPList.ps1"
The PowerShell script that is running:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
$currentIpAddress = Test-Connection $env:COMPUTERNAME -count 1 | select Address, Ipv4Address
$csv = Import-Csv C:\Temp\RenameComputerBasedOnIPList.csv
$newComputerName = $csv | where {$_.IPADDRESS -eq $currentIpAddress.IPV4Address} | % NEWCOMPNAME
Write-Host $currentIpAddress
Write-Host $csv
Write-Host $newComputerName
Rename-Computer -NewName $newComputerName -Force -Restart
The formatted list is like this named RenameComputerBasedOnIPList.csv.
IPADDRESS,NEWCOMPNAME
10.96.21.121,BADCOMPNAME
10.96.21.158,WIN10NAMECHANGE
192.168.0.2,BADCOMPNAME1
10.96.21.52,WIN81NAMECHANGE
Thanks again.
I just wrote a powershell script that will export dhcp lease information but i want to export specific information like export only IP and mac addresses in the dhcp. Instead of exporting every lease information. The one line of code i have written that exports everything is bellow.
Get-DhcpServerv4Lease -ComputerName "HW2009-11" | Export-Csv -Path ("C:\log\new.csv")
To adress only certain properties of an object, you can use Select-Object. This way you can only choose the ipand mac-address like this:
Get-DhcpServerv4Lease -ComputerName "HW2009-11" | Select-Object -Property IP, mac-address
You can then pipe this to Export-Csv and it will create a .csv file with only those properties:
Get-DhcpServerv4Lease -ComputerName "HW2009-11" | Select-Object -Property IP, mac-address | Export-Csv -Path "C:\log\new.csv"
If you don't know the specific properties of an object, you can just pipe the command to Get-Member:
Get-DhcpServerv4Lease | Get-Member
I am attempting to search HotFixes installed on a machine and pipe the machine name with the specified KB installed to an excel sheet "else" pipe the machine names without the KB installed to another excel sheet.
I've gotten the basic command down that will do this but because it uses the % (foreach) command, it is appending the machine name to the excel sheet for each KB it finds or doesn't find and I end up with dozens of duplicates.
How can I get this code to append a single entry for each search through all the KBs and not for each KB?
Get-HotFix | %{if($_.HotFixID -match "KB2687455"){(get-wmiobject win32_computersystem).name | Out-File C:\Installed.txt -append}` else{(get-wmiobject win32_computersystem).name | Out-File C:\NotInstalled.txt -Append}}
EDIT:
I have found a work around. Instead of trying to do this in a command line type of code I rewrote the code and surprisingly it worked. Let me know if you see any potential issues with this code.
if (Get-HotFix |?{$_.HotFixID -match "KB2687455"}) {
(get-wmiobject win32_computersystem).name | out-file C:\Installed.txt
}
else {
(get-wmiobject win32_computersystem).name | out-file C:\NotInstalled.txt
}
You are looping over the hot fixes that are installed, and writing results for each hot fixes when it looks like all you want to do is write whether a given machine has a hot fix installed or not. I'm assuming you will be running this on multiple machines and that the output file will be on a network share.
You need to break out of your else block after the initial pass:
Get-HotFix | % {
if ($_.HotFixID -match "KB2687455") {
(get-wmiobject win32_computersystem).name | Out-File C:\temp\Installed.txt -append
}
else
{
(get-wmiobject win32_computersystem).name | Out-File C:\temp\NotInstalled.txt -Append
break
}
}
I am using net use to get the Windows mount path and drive.
PS C:\Users\jagg> net use
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK Y: \\ITHANSJJA001.ABC.COM\opmas$
Microsoft Windows Network
The command completed successfully.
I would like to fetch the drive and mount path detail using PowerShell command. Is there any way to get it using only PowerShell?
you seem to be confused about what powershell is able to do. [grin]
however, here are two ways to get the info you seem to want. the 1st parses the output of net use while the 2nd uses Get-PSDrive to get that same info natively.
(net use) -replace '\s{2,}', ',' |
Select-String -SimpleMatch '\\' |
ConvertFrom-Csv -Header 'Status', 'DriveLetter', 'MountPath', 'Network' |
Select-Object -Property DriveLetter, MountPath
''
Get-PSDrive -PSProvider FileSystem |
# the 4 slashes are 2 regex-escaped slashes
Where-Object {$_.DisplayRoot -match '\\\\'} |
ForEach-Object {
[PSCustomObject]#{
DriveLetter = '{0}:' -f $_.Name
MountPath = $_.DisplayRoot
}
}
hope that helps,
lee