Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

Weekend Scripter: The Easy Way to Manage Disk Drives

$
0
0

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell 3.0 to manage disk drives on his Windows 8 laptop.

Microsoft Scripting Guy, Ed Wilson, is here. Today, the Scripting Wife and I are back in Dortmund, Germany. We will spend the day with Windows PowerShell Guru Klaus Schulte. Our first day in Germany was spent in Dortmund where we had dinner at the Florian tower with Klaus, his lovely wife, Susanne, his daughter, Micah, and two sons, Janus and Marcos. The Florian tower is in the Westfalenpark, which has a great botanical garden, lots of jogging trails, and neat places for walking. The Florian tower is shown here.

Image of Florian tower

Checking the status of disk drives by using Windows PowerShell

During our European trip, I am using an external disk drive I purchased to store all of the pictures I have been taking. My new camera stores both RAW and JPG picture formats, so each picture I take consumes nearly twice the space. My laptop drive is small, and therefore, I thought an external drive would be a perfect solution for the trip.

The drive and my laptop both support USB 3.0, so speed works really well. However, there is one problem—external disks, such as the USB key fobs, appear removable to Windows. The big disk drive appears as a fixed disk, so I must go into Disk Manager to mount and dismount the drive. If I do not dismount the drive prior to shutting the laptop down, it searches forever on boot up, looking for the external drive that is missing. Using Disk Manager is a bit slow and involves the following steps.

  1. I have to go to the Start screen.
  2. Scroll over to Find and click the Administrative Tools tile.
  3. Locate and click the Computer Management MMC.
  4. Find the Storage section, and click Disk Management.
  5. After Disk Management appears, right-click the offline drive.
  6. Make it online.

Of course, there is an easier way, just type diskmgmtin the Windows PowerShell console, and the Disk Management MMC appears. Of course, one must remember that the MMC for Disk Management is diskmgmt.

Disk Management is shown here and shows my offline disk drive.

Image of Computer Management

An easier way to check on the status of disk drives is to use the Get-Disk function (cmdlet) on Windows 8 (and also on Windows Server 2012). To do this, I only need to type Get-Disk, and a list of drives is displayed in the Windows PowerShell console. This is shown here (without my extra drive plugged in).

PS C:\> Get-Disk

Number Friendly Name  Operational Status  Total Size Partition Style

------ -------------                            ------------  ---------- -----------

0      INTEL SSDSA2BW160G3L      Online       149.05 GB MBR

Note  Using the Get-Disk function (cmdlets) requires Admin rights. If you run Get-Disk without Admin rights, the rather cryptic error message “Access to a CIM resource was not available to the client" appears.

When I add in my external USB disk drive and re-run the command, I see that the new drive is offline, as shown here.

PS C:\> Get-Disk

Number Friendly Name  Operational Status  Total Size Partition Style

------ -------------                            ------------  ---------- -----------

0      INTEL SSDSA2BW160G3L                     Online         149.05 GB MBR

1      SAMSUNG HN-M101MBB USB Device            Offline        931.51 GB MBR 

Bring on all offline disk drives

It is easy to bring online all offline disk drives. All I need to do is to use the Get-Disk function to retrieve all disk drive information, pipe the results to a Where-Object cmdlet to filter out for isoffline disk drives. I do this first to ensure I retrieved the intended disk drive, as shown here.

Note   I have used the new Windows PowerShell where object syntax, but this command only works on Windows 8 and Windows Server 2012, so it is not an issue. Oh, by the way, ? is an alias for the Where-Object and has been since Windows PowerShell 1.0.

PS C:\> Get-Disk | ? isoffline

Number Friendly Name  Operational Status  Total Size Partition Style

------ -------------                            ------------  ---------- -----------

1      SAMSUNG HN-M101MBB USB Device            Offline        931.51 GB MBR

Perfect! Now I only need to retrieve the command, and then pipe the disk object to the Set-Disk function and specify that I want the value of the IsOfflineproperty to be False in order to bring the drive online, as shown here.

Get-Disk | ? IsOffline | Set-Disk -IsOffline:$false

After I run the command, the new disk drive springs to life, and Windows Explorer opens to reveal the drive contents, so I know it worked. BUT, if I were not certain, I could always reuse the Get-Disk cmdlet, as shown here.

PS C:\> Get-Disk

Number Friendly Name  Operational Status  Total Size Partition Style

------ -------------                            ------------  ---------- -----------

0      INTEL SSDSA2BW160G3L                     Online         149.05 GB MBR

1      SAMSUNG HN-M101MBB USB Device            Online         931.51 GB MBR

OK. Everything looks groovy. But to be sure, I want to view all the information about my newly “installed” drive. So I pipe the results to the Format-List cmdlet (fl is an alias for this cmdlet). The command and the associated results appear in the following image.

Image of command output

Writing to read-only drives

For some reason, the first time I did this, the drive came online as read-only. When I attempted to dump my pictures from my camera, I obviously did not have a very good experience. I finally figured it out. One of the interesting things about the Set-Disk cmdlet is that there are four parameter sets to bring a drive online or offline. These parameter sets are shown here.

Set-Disk [-Number] <UInt32> [-AsJob [<SwitchParameter>]] [-CimSession

<CimSession[]>] [-IsOffline <Boolean>] [-ThrottleLimit <Int32>]

[<CommonParameters>]

 

Set-Disk [-AsJob [<SwitchParameter>]] [-CimSession <CimSession[]>] [-IsOffline

<Boolean>] [-ThrottleLimit <Int32>] -Path <String> [<CommonParameters>]

 

Set-Disk [-AsJob [<SwitchParameter>]] [-CimSession <CimSession[]>] [-IsOffline

<Boolean>] [-ThrottleLimit <Int32>] -UniqueId <String> [<CommonParameters>]

 

Set-Disk [-AsJob [<SwitchParameter>]] [-CimSession <CimSession[]>] [-IsOffline

<Boolean>] [-ThrottleLimit <Int32>] -InputObject <CimInstance[]>

[<CommonParameters>]

Another four parameter sets permit making a drive read-only or not. These are shown here.

Set-Disk [-Number] <UInt32> [-AsJob [<SwitchParameter>]] [-CimSession

<CimSession[]>] [-Guid <String>] [-IsReadOnly <Boolean>] [-Signature <UInt32>]

[-ThrottleLimit <Int32>] [<CommonParameters>]

 

Set-Disk [-AsJob [<SwitchParameter>]] [-CimSession <CimSession[]>] [-Guid

<String>] [-IsReadOnly <Boolean>] [-Signature <UInt32>] [-ThrottleLimit

<Int32>] -Path <String> [<CommonParameters>]

 

Set-Disk [-AsJob [<SwitchParameter>]] [-CimSession <CimSession[]>] [-Guid

<String>] [-IsReadOnly <Boolean>] [-Signature <UInt32>] [-ThrottleLimit

<Int32>] -UniqueId <String> [<CommonParameters>]

 

Set-Disk [-AsJob [<SwitchParameter>]] [-CimSession <CimSession[]>] [-Guid

<String>] [-IsReadOnly <Boolean>] [-Signature <UInt32>] [-ThrottleLimit

<Int32>] -InputObject <CimInstance[]> [<CommonParameters>]

Unfortunately, there are no parameter sets that permit bringing a disk online AND making it read-write at the same time. So if the drive comes online in read-only fashion, I need to use the Set-Disk cmdlet a second time to make it read-write. This is shown here.

Get-Disk -FriendlyName *usb* | Set-Disk -IsReadOnly:$false

Of course, I can combine the two commands as a series when bringing the drive online, as shown here.

Get-Disk | ? IsOffline | Set-Disk -IsOffline:$false

Get-Disk | ? IsReadOnly | Set-Disk -IsReadOnly:$false

When I am finished uploading pictures to my drive, and I want to put the drive away, I run the command shown here to take the drive offline, so I can unplug it. Here is the command I use.

Get-Disk -FriendlyName *usb* | Set-Disk -IsOffline:$true

See you tomorrow as the Scripting Wife and I travel to Cern to visit the Particle Accelerator. My blog post will discuss using the CIM cmdlets to explore WMI classes—it's good stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>