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 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 

Comments (8)

  1. happysysadm says:

    Hi Ed,

    interesting post.

    While I agree with Igor on the fact that you don't need to disable defrag because Windows already skips this kind of drives (and that you may prefer to move pagefile on a mechanical drive instead of fixing its size), I still think that what's important here is that you show us how to do it.

    Very instructive!

    Carlo

  2. happysysadm says:

    @Massimo I see your point. In that case I disable paging if I have enough RAM. Maybe SSD short life expectancy  is only a urban myth but I still prefer to avoid too many "blocks" being modified.

    Carlo

  3. Igor Kudrin says:

    Why disable "defrag"? Windows 8 does not defrag SSD, it is doing some kind of optimization.

  4. Massimo says:

    I love this post!

    My 2 cents: I agree on disabling hiberfile.sys… since I put a SATA3 SSD drive, my laptop clean boots in less than 10 seconds, I've never hibernated my laptop since I moved to SSD.

    I do not agree with "happysysadm" because, if you have a laptop with a single drive it is pretty likely that you only have 1 drive, so, no chance to move to a second disk 😉

  5. Jonathan Walz says:

    After that you could always use Chocolatey.org to install a bunch of your commonly used apps with a simple PowerShell script. 🙂 Thanks Ed.

  6. Ed Wilson says:

    @Igor Kudrin Thanks for this information. I was looking at it like Windows 7 and when I saw the task was not disabled, I went ahead and disabled it. I will turn it back on.

    @Happysysadm moving the page file is a good idea, and when I get my second drive running on my laptop I will consider that.

    @Massimo Thanks, I am glad you love the post. Yes, I feel that reclaiming the disk space on my laptop was more important than the small gains used by hiberfile. You are right I feel that Windows 8 boots up very quickly anyway.

    @Jonathan Waltz great suggestion thanks.

  7. Massimo says:

    @HappySysAdmin…. are you sure that disabling page file is a good idea? I always thought that pagefile is somewhat "mandatory". I started thinking this when I disabled the pagefile on a server with exchange 2003 and he didn't react very well.

    Actually I don't know if disabling it (even when you have much physical RAM) is a good idea or not.

    @Ed, with modern SSD, limited writes are a actually a mith. My first Corsair SATA3 (6gbps) 120 GB SSD lived 1 year and a half on my laptop. When I upgraded to Corsair SATA3 240 GB, one year ago, I moved the 120 GB drive to my desktop workstation to run virtual machines faster. That disk is still perfect.  🙂

Skip to main content