Quantcast
Viewing all articles
Browse latest Browse all 36188

Weekend Scripter: Use PowerShell to Configure a New Laptop

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to configure a new laptop.

Microsoft Scripting Guy, Ed Wilson, is here. WooHoo! My new laptop arrived. Oh, no…I now have a lot of work to do to install and configure Windows 8 on it. You see, I never, never simply turn on and use a new laptop—that is not the way I operate. I always do a fresh installation. I am not saying one needs to always do this, but I like to know what is going on and make my own decisions. So here I am with a lot of work to do.

Setting up the new laptop

A while back I began working on a new computer deployment module that helps me configure a number of things that simply irate me about default installs. Today I am going to discuss a few types of things that I need to configure.

I do not want to defrag my SSD drive

This one is easy. In Windows 8, there is a Scheduled Task module, and all I need to do is disable one scheduled task. When I first tried the command, I received the following error message:

PS C:\> Get-ScheduledTask -TaskName *defrag* | Disable-ScheduledTask

Disable-ScheduledTask : Access is denied.

At line:1 char:40

+ Get-ScheduledTask -TaskName *defrag* | Disable-ScheduledTask

+                                        ~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : PermissionDenied: (PS_ScheduledTask:Root/Microsoft/..

   .S_ScheduledTask) [Disable-ScheduledTask], CimException

    + FullyQualifiedErrorId : HRESULT 0x80070005,Disable-ScheduledTask

When I started Windows PowerShell with Admin rights, the command came off without a hitch as shown here.

PS C:\> Get-ScheduledTask -TaskName *defrag* | Disable-ScheduledTask

 

TaskPath                                       TaskName                          Sta

                                                                                 te

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

\Microsoft\Windows\Defrag\                     ScheduledDefrag                   Dis

I do not want to hibernate

Windows 8 has a cool feature called Fast Start. Unfortunately, I have a scavenged SSD in my laptop that is really small, so I cannot afford the disk space of Hiberfile.sys. Anyway, guess what? Windows 8 on an SSD boots up pretty quickly anyway. So this is a simple one-line command using the PowerCfg command-line tool.

Powercfg /H OFF

Note   This command also requires Admin rights. If you run it without Admin rights, the following error arises:

PS C:\> powercfg /H OFF

An unexpected error condition has occurred. Unable to perform operation. You may not have permission to perform this operation.

I want to manage my own page file

If I use Get-CimInstance to query for instances of Win32_PageFileSetting, guess what? It returns nothing—not a thing. There is not an instance of Win32_PageFileSetting because Windows is managing the page file.

Disable auto management of the page file

So the first thing I need to do is to turn off auto management of the page file. I use the Win32_ComputerSystem WMI class to do this. Here is the command.

PS C:\> Set-CimInstance -Query "Select * from win32_computersystem" -Property @{autom

aticmanagedpagefile="False"}

PS C:\> Get-CimInstance win32_PageFileSetting

 

                 MaximumSize Name                        Caption

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

                           0 C:\pagefile.sys             C:\ 'pagefile.sys'

Specify the size of the page file

To set the size of the page file, I can now use the Set-CimInstance cmdlet, and set the size by using the Win32_PageFileSetting cmdlet. Here is the script I used.

PS C:\> Set-CimInstance -Query "Select * from win32_PageFileSetting" -Property @{Init

ialSize=3072;MaximumSize=3072}

It took a few times before I came up with the right combination. In the end, I looked up the Win32_PageFileSetting WMI class on MSDN, and I found that it wants the values in megabytes.

Image may be NSFW.
Clik here to view.
Image of command output

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. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Image may be NSFW.
Clik here to view.

Viewing all articles
Browse latest Browse all 36188

Trending Articles