Recent Posts

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.

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: