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:
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.
- 10 cool things you can do with Windows PowerShell
- Pop Quiz: Microsoft Windows PowerShell basics
- Simplify PowerShell script creation with PowerGUI
- Managing Active Directory objects with PowerShell in Windows Server 2008
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!