Ezine 168 – Mapping Drives and Printers with PowerShell
This week’s scripts illustrate the advantages of transferring from PowerShell most eloquently. VBScripters will recognise the following CreateObject command, and I hope they can see the similarities in the way that PowerShell maps to printers.
Set objNetwork = CreateObject("WScript.Network") [VBScript]
new-Object -com WScript.Network [PowerShell]
Topics for Mapping PowerShell’s Printers and Network Drives
- This Week’s Secret
- This Week’s Mission
- Example 1: To Add or ‘Map’ a Network Printer
- Example 2: ‘Map’ a Network Drive
- More on COM Objects
♣
This Week’s Secret
Back in the year 2,000 when I first published my website I noticed that the pages on mapping network drives and mapping printers generated the most hits. From that basic market research I developed a whole section on VBScript logon scripts.
Over time I have faced a dilemma, on the one hand my readers clamber for more on mapping drives and printers, while on the other hand I have a vision of Group Policies providing better methods for giving users access to printers and shared resources.
This Week’s Mission
This Week’s Mission is split into two sections. Firstly, a ‘refresher’ on adding or mapping a printer. If you remember the idea is that you have a printer attached to server and you want the users to be able to ‘map’ to this network printer; as a result they can get hard copies of their documents.
Secondly, to appreciate how just five words of PowerShell build a network object, which we then can manipulate in all manner of ways. That one-line is:
new-Object -com WScript.Network. [Note the only two spaces are before -com, and between -com and WScript].
As aside I hope that you can see how easy it is to transfer VBScript skills to PowerShell.
Preliminary Instructions to Run PowerShell code:
If you have not used PowerShell before, here are step-by-stepinstructions to execute commands.
Method 1 (Quick)
- Launch PowerShell
- Copy the code into memory
(For instance, from Example 1 below) - Right-click on the PowerShell symbol
- Edit –> Paste
- Press enter to execute the code
- See screenshot to the right
Method 2 (Best)
- Prepare to run cmdlets with this PowerShell command:
set-ExecutionPolicy RemoteSigned - Copy the code below into a text file.
- Save the file with a .ps1 extension, for example: printer.ps1
- In PowerShell, navigate to where you saved printer.ps1
- Issue this command:
.\network
(dot backslash filename)
Example 1: To Add or ‘Map’ a Network Printer
It was a coin toss whether to start with map network drive, or add printer. I came down on the side of the printer because it followed on from last week’s script.
Preliminary step: To get this script to work on your computer you must amend ‘Server\PrintShare’ to reflect the name of a real server and a real printer on your network.
# PowerShell for Mapping a Printer
# Author: Guy Thomas
# Version 1.3 July 2008 tested on PowerShell v 1.0
$PrinterPath = "\\Server\PrintShare"
$net = new-Object -com WScript.Network
$net.AddWindowsPrinterConnection($PrinterPath)
Note 1: Did you edit the value for $PrinterPath to reflect a computer on your network?
Note 2: At the heart of this PowerShell script is:
New-Object -com WScript.Network.
Note 3: Troubleshooting There should be no space before the bracket, I realized that for once there was no spelling mistake in AddWindowsPrinterConnection , just an unwanted space, to see what I mean here is the error amplified in the line below:
$net.AddWindowsPrinterConnection ($PrinterPath).
Note 4: Talking of brackets, in PowerShell the type of bracket is always highly significant, the (parenthesis style) means a compulsory component. PowerShell’s {curly brackets} pronounce:- script block inside. Occasionally PowerShell employs a third type, [square brackets are optional]. Thus the type of bracket gives extra information, and all these nuances make scripts easier to read and easier to troubleshoot.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Example 2: ‘Map’ a Network Drive
As usual, preparation and orientation are the keys to a successful script, in this instance amend the value for the variable $Share. I seriously doubt you have a server called ‘server’ and a share called ‘share’.
# PowerShell for Map a Network Drive
# Author: Guy Thomas
# Version 2.2 July 2008 tested on PowerShell v 1.0
$Drive = "Y:"
$Share ="\\server\share"
$Net = New-Object -com WScript.Network
$Net.mapnetworkdrive($Drive,$Share)
Note 1: How did I know about the .mapnetworkdrive method? The answer is once I created the com object called $Net, I tried this command:
$Net | get-Method
Note 1a: The above get-Method (gm) technique reveals more .net methods, for instance, AddWindowsPrinterConnection which we used in Example 1.
Note 2: As I hinted earlier, I have a whole section describing the syntax for MapNetworkDrive and the other com objects. The point of this script is to get you started emulating, then replacing VBScript with PowerShell.
Guy Recommends: Tools4ever’s UMRA
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.
Summary of PowerShell Mapping Drives and Printers
I sympathise with those who are reluctant to abandon their VBScript skills in favour of PowerShell. However, I justify becoming a dripping tap and preaching, ‘Switch to PowerShell’, because I have taken this route myself and I am reaping the benefits.
As one level this article explains how to map a drive or a printer using PowerShell. At other level it shows how easy it is to create .net objects and thus assist you make the transition from VBScript to PowerShell.
If you like this page then please share it with your friends
See more Microsoft PowerShell tasks:
• PowerShell Home • Shell Application • New-Object • PowerShell Add Printer • PowerShell -com
• PowerShell Logon Script • Map Network Drive • PowerShell Create Shortcut • Free CSV Import Tool
• Invoke-Expression • Invoke-Command • Invoke-Item • PowerShell Expression v Command Mode
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.