Here are three very simple task which were hard to achieve using the MS-DOS shell.
Connect to UNC paths
To browse a network share in cmd.exe you first had to map a drive. With PowerShell you can navigate to server shares as you would a local drive.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set-location \\\\servername\sharename |
Easy to recurse folders and files
While it is possible to recursively delete files in cmd.exe, PowerShell has built in support for the**
operator making operations on sets of files very simple. For an example, the following will delete all files with the extension .bak from the C:\temp and all folders within it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
remove-item c:\temp\**\*.bak |
Controlling remote servers
Often I have to administer a service on a remote machine, a recent example is starting the W3SVC on a server which kept failing. As PowerShell has access to the Windows Management Interface API I can run this command instead of using IIS manager over Remote Desktop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Get-WmiObject -computer servername Win32_service -Filter "Name='W3SVC'").StartService() |
In fact PowerShell finally provides a useful wrapper around those rich but hard to get at WMI APIs. The following script will enumerate all websites and display the path to their log file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-WmiObject -namespace "root/MicrosoftIISV2" -ComputerName "Server" -Credential "yourname" -Authentication PacketPrivacy -Query "SELECT * FROM IIsWebServerSetting" | select { $_.ServerComment, $_.LogFileDirectory } |