Windows

Update Windows scripts using these handy PowerShell commands

It may be time to upgrade batch files to PowerShell for automating the role of administering a Windows Server. Rick Vanover provides commands to use when updating Windows scripts.

Like many administrators, I have numerous scripts written as .BAT batch files that could use an update. Now that Windows Server 2008 R2 has PowerShell 2 installed by default, it is time.

Let's take a few of the staples that we have put in .BAT files, and I'll give you the PowerShell equivalent.

Reboot a remote computer

To perform a 10-second delay on a remote server (Server1), the following is entered in a batch file:

shutdown /r /m \\SERVER1 /f /t 10

In a PowerShell file, the equivalent is:

Start-Sleep 10
Restart-Computer -Force -ComputerName SERVER1

Restart a service

To restart the DNS cache service on a Windows Server, the following would have been entered in a batch file:

sc stop dnscache
sc start dnscache

Within PowerShell, the equivalent is:

Restart-Service dnscache

Map a drive

In a DOS window or a batch file, the following command would be used to map a drive to a network computer:

Net use L: \\server1\c$

With a PowerShell prompt, the equivalent is:

New-PSDrive -name L -psprovider FileSystem -root \\server1\c$
Note: A PSDrive is not the same as other drives; this file system space is contained to PowerShell.

While these are the simplest commands, if you replace your day-to-day scripts with PowerShell, you will be equipped with better scripts. These scripts can include richer automation options, yet still be launched interactively in PowerShell.

For additional resources, check out the PowerGUI community, the Windows PowerShell homepage, and these TechRepublic blogs:

Stay on top of the latest Windows Server 2003 and Windows Server 2008 tips and tricks with our free Windows Server newsletter, delivered each Wednesday. Automatically sign up today!

About

Rick Vanover is a software strategy specialist for Veeam Software, based in Columbus, Ohio. Rick has years of IT experience and focuses on virtualization, Windows-based server administration, and system hardware.

5 comments
TRichFCAR
TRichFCAR

I appreciate this info.. I have a dillema I can't solve. I need help. My other laptop is showing this..... \WINDOWS\SYSTEMS32\CONFIG\SYSTEM.SAV What do I do? Please give me direction.

jamey123
jamey123

I notice that most of the PS scripts require more key strokes... :(

jfkvault97
jfkvault97

Ive notice using scripts a great tool and more handy with shell commands as well, saves time to get users going as well as connecting to various requirements with in a large-medium business network, though I have to say at first it is confusing but after awhile a big smile for to work smart and not to work hard hahahaha

randy_scadden
randy_scadden

I'm glad I wasn't the only one that noticed that. Especially when mapping a network drive. Does anyone know if using the PS equivalent if that does a persistent map or does the script have to be run every time a system logs in? The only reason I ask is b/c if I switch to PS I'm going to have a lot of remote users who are going to be ticked off if they aren't able to get out to their mapped network drives.

twometertwo
twometertwo

You can create aliases for common commandlets that will allow you to reduce the amount of key strokes. For example the question mark is an alias for Where-Object the Percent sign is an alias for Foreach-Object. Try the alias and scroll through the list of aliases. This will help to reduce the amount of keystrokes if you're that worried about it. One Caveat, make sure to use the full cmdlet name within saved scripts that way if you share it with others, it will be easier to interpret.

Editor's Picks