Category Archives: Windows 10

Configure Storage Sense using PowerShell

Recently the Storage Sense functionality has been added to Windows. Unfortunately there are no PowerShell commands available to manage this so I decided to write this function to be able to manage the different settings that are available in regards to Storage Sense.

There are a number of settings that can be configured in the Settings > Storage > Storage sense, settings menu. The settings that can be configured are as follows:

By clicking: Change how we free up space, the following options become available:

So in order to configure this using PowerShell I have created the Set-StorageSense function with the following parameters:

  • EnableStorageSense
  • DisableStorageSense
  • RemoveAppFiles
  • ClearRecycleBin

So if you would like to disable this functionality and not have Windows automatically clear out temporarily and otherwise obsolete files you can run the following command:

1
Set-StorageSense -DisableStorageSense

Alternatively the other options can also be configured, the following command Enables Storage Sense and configures all cleanup options to true while displaying verbose output:

1
2
Set-StorageSense -EnableStorageSense -RemoveAppFiles $true`
-ClearRecycleBin $true -Verbose

This would of course not be complete without being able to query this data as well, which is possible using the Get-StorageSense function:

Although I looked into manually triggering the Clean Up job, this is something that did not seem possible, I used Sysinternals Procmon to determine which command is associated with that. But even when running this as the System account it did not appear to clean anything. I will provide the execution in case it is of value for anyone else:

c:\windows\system32\svchost.exe -k localsystemnetworkrestricted -s StorSvc

Let me know if PowerShell functions like these are useful to you and if it works as expected. Do note that this functionality is only available in recent builds of Windows 10.

Links in this Article
GitHub – Get-StorageSense
GitHub – Set-StorageSense
GitHub – CustomizeWindows10 Module
PowerShellGallery – CustomizeWindows10 Module

 

Share

Windows 10 – Start Windows applications from Bash

Working with Bash on Windows, I can say that it significantly simplified the way I can work cross-platform. It has allowed me to use native Linux tools, when working with Linux systems, which is a big step up from using a mix of compiled for Windows Unix tools and GUI applications. Because of this unique functionality Windows 10 has a big lead over any other platform as my day-to-day platform.

For example it is possible to add a new Windows users account directly by using the net.exe tools:

1
net.exe user /add testaccount

Alternatively it is also possible to start Windows PowerShell in Bash to execute a command, for example it becomes possible to use PowerShell to determine which processes are running in the Windows environment and the ps command to determine what is running in Linux:

1
2
powershell.exe -nop -command "Get-Process | Select-Object -First 10"
ps -aux

For me the greatest advantage is the fact that Windows executable files can also be directly called from bash, simplifying the interaction between the different OSes. An example of this is opening explorer in your current working folder in the bash terminal:

1
explorer.exe .

When a path is not accessible for the Windows application, for example anywhere in the Unix file system, a proper error message will be displayed:

1
explorer.exe .

Using this same method it is also possible to add the path to your favorite script editor to the Bash on Windows path allowing them to be directly called from the console. In the following example I will show how to open VSCode directly from Bash:

1
2
3
ls README.md
export PATH=$PATH:/mnt/c/Program\ Files\ \(x86\)/Microsoft\ VS\ Code/
Code.exe README.md

For more information and example of what is possible with the Bash commandline on Windows or I recommend following the Windows Command Line Tools For Developers blog on MSDN.

Share

Configure Windows 10 Blue Light settings with PowerShell

As discussed in the previous post on configuring Lunar Calenders in Windows 10, another interesting feature was also introduced in the 15002 Windows 10 Build: Blue Light Settings. Recently more operating systems have included options for reducing the amount of blue light emitted after sundown. This is easier on your eyes and allows you to fall asleep better at night time.

There are a number of settings that can be configured in the Settings > Display > Blue light settings, settings menu. The settings that can be configured are as follows:

Because automation is a great thing and manually clicking through GUI interfaces is not always optimal I decided to write a PowerShell function to simplify the configuration. For example you could utilize this function to automatically disable the reduced blue light settings when color sensitive work is performed, or lower the amount of blue light emitted as the night progresses. These are some of the actions that the Set-BlueLight function can perform.

To instantly turn of the reduced blue light setting the following code can be run:

1
Set-BlueLight -DisableBlueLight

To enable the reduced color mode and set the color shift to Medium run the following code:

1
Set-BlueLight -EnableBlueLight -ColorTemperature MediumShift

To automatically reduce blue light emitted based on the Day and Night cycle in your geographical location execute this:

1
Set-BlueLight -EnableAutomaticSchedule

The values that are configured by the script are byte arrays in the registry, so it required a bit of reverse engineering to get the values to match the settings in the control panel. The function is currently available on GitHub in my Shared Scripts repository: Set-BlueLight

This function will be added to the CustomizeWindows10 module after I have developed my functional and unit tests to ensure the quality of this function.

For more information about the functions and modules discussed in this article, please refer to the following links:

Links in this Article
GitHub – Set-BlueLight
GitHub – CustomizeWindows10 Module
PowerShellGallery – CustomizeWindows10 Module

 

Share

Enable Lunar Calendar in Windows 10

As I installed the latest build of Windows 10 on my system I read that it was now possible to set the calendar to also display the Lunar Calendar in the calendar overview. Because I like automating stuff I decided to take a look at where this is configured. Note that the steps in this post are based on Windows 10 Build 15002,

It turns out this in configured in the following registry key:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\CloudStore\Store\Cache\DefaultAccount\$$windows.data.lunarcalendar\Current

Interestingly enough, the store cache seems to contain many of the recently released new functionality in Windows 10. At the moment this seems to be the space to watch if you are looking to automate your settings in Windows, although most values are binary so it might take some work to figure out how to configure this.

After setting this value to the three possible options:

  • Off
  • Lunar Calendar with simplified Chinese characters
  • Lunar Calendar with traditional Chinese characters

Unfortunately this because the Data property is a binary string, so this is not that easy to manipulate. The following values correspond with the settings. I will list both the binary strings as well as their base 64 representation.

Settings Byte Array Base64
Off 2,0,0,0,88,33,207,247,241,107,210,1,0,0,0,0,67,66,1,0,16,2,0 AgAAAFghz/fxa9IBAAAAAENCAQAQAgA=
Simplified 2,0,0,0,148,217,114,130,241,107,210,1,0,0,0,0,67,66,1,0,16,4,0 AgAAAJTZcoLxa9IBAAAAAENCAQAQBAA=
Traditional 2,0,0,0,75,55,152,236,241,107,210,1,0,0,0,0,67,66,1,0,16,6,0 AgAAAEs3mOzxa9IBAAAAAENCAQAQBgA=

To simplify settings these values the Set-AdditionalCalendar function that is included in the CustomizeWindows10 module can be used to configured these settings:

To install this module and configure these settings run the following code:

1
2
Install-Module -Name CustomizeWindows10
Set-AdditionalCalendar -Simplified

For more information about the functions and modules discussed in this article, please refer to the following links:

Links in this Article
PowerShellGallery – CustomizeWindows10 Module
GitHub – CustomizeWindows10 Module
GitHub – Set-AdditionalCalendar
Share

Path of Linux User-Mode filesystem in Windows 10

Over the past few weeks I have been using the new functionality in the latest Windows 10 insider builds that allows you to run Ubuntu in User Mode on Windows 10. Or as it is also known: Windows Subsystem for Linux. The file system of Ubuntu is located at the following location:

%userprofile%\appdata\local\lxss

To navigate to this location from PowerShell the following code can be used:

1
2
Set-Location -LiteralPath 'C:\Users\Jaap Brasser\appdata\local\lxss\'
Set-Location -LiteralPath "${env:userprofile}\appdata\local\lxss\"

LinuxBashonWindows

By accessing this location it is easy to copy files from the Windows filesystem directly into the Linux file system.

For more information in regards to Linux User-Mode on Windows 10, please refer to the following links:

Links in this Article
Blog – Windows Command Line Tools For Developers
Announcing Windows 10 Insider Preview Build 14316
Video – Running Bash on Ubuntu on Windows!

Share

DiskCleanup – Remove Previous Windows Versions – PowerShell Module

Over the last few weeks I have upgraded and reinstalled a number of Windows 10 machines and as part of my routine after an upgrade to a later build of Windows 10 I would clean up both the Upgrade files as well as the old version of Windows. The steps to do this involves starting the Disk Cleanup tool, with Administrative credentials, and clicking through the interface in order to clean up the files and save up some of those precious GBs of storage space. After repeating this process a few times in the past week it started to become a bit tedious.

In order to do this is use the command line options that are available with the Disk Cleanup tool, cleanmgr.exe. While it is unfortunately not possible to use this tool directly to clean up specifically the left over files of an Upgrade of Windows 10, it does allow for using the GUI to create a ‘StateFlags’ which is stored in the registry. Using this methodology in combination with Sysinternals Procmon I established where this information was written in the registry and I came up with the following three lines of PowerShell code that allowed me to create the job required to do this job:

1
2
3
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -PropertyType 'DWORD' -Force -Name 'StateFlags1337' -Value 0x2
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -PropertyType 'DWORD' -Force -Name 'StateFlags1337' -Value 0x2
cleanmgr.exe /SAGERUN:1337

This will code will first create two registry keys and after creating those registry keys, specify the Disk Cleanup application to run with job number 1337. This worked, although unfortunately it is not possible to hide the GUI completely as user interaction might still be required. In certain scenarios you will be prompted to confirm that you really do want to delete the Old Windows Installation files, that includes the warning that deleting those files will not allow you to roll back to an older version of Windows anymore. This also includes the files if you upgrades from Windows 7/8.1 to Windows 10.

As I saw more potential for this method of utilizing the Disk Cleanup tool, I decided to write a proper module that does simplifies the job creation process beyond just cleaning up Previous Windows Installation / Upgrade files. Today I have released the module that does contains the following functions:

  • Get-VolumeCachesKey
  • Get-VolumeCachesStateFlags
  • Remove-WindowsUpgradeFiles
  • Set-VolumeCachesStateFlags

To install the module on your system you can run the following code:

1
Install-Module DiskCleanup -Verbose

InstallModuleDiskCleanup

To remove  the Previous Windows Installation / Upgrade files run the following command:

1
Remove-WindowsUpgradeFiles -Verbose -Confirm:$false

RemoveUpgradeFiles

This will both create the job with number 1337 and then execute that job, and the Disk Cleanup Windows will appear on your screen. This process can take a few minutes depending on the speed of your system. The function will wait until all Disk Cleanup windows have been closed and then show a report of the amount of space saved.
DiskCleanup

Use the Get-VolumeCachesStateFlags cmdlet it is possible to view what settings have been configured:
Get-VolumeCachesStateFlags

It is also possible to setup a custom job using the Set-VolumeCachesStateFlags cmdlet, the mandatory parameter -StateFlags will accept any number of Switch parameters. These parameters are dynamically generated based on the available Keys in the registry path:

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files

SetVolumeCachesStateFlags

The module is available at the following online repositories:

I will actively maintain this module, so feel free to leave a comment or feature request in the comments or send in a pull request on GitHub.

All the links mentioned in this article are available below:

Links in this Article
PowerShell Gallery – DiskCleanup
TechNet Script Gallery – DiskCleanup
GitHub – JaapBrasser – DiskCleanup
Sysinternals – Process Monitor

 

Share

Quicktip: PowerShell command history on Windows 10 using PSReadline

In Windows 10 PowerShell 5.0 comes bundled with PSReadline. This module allows for a number of useful features and today we will focus on getting access to the command history. A very useful feature of PSReadline is that it writes the history of PowerShell commands to a text file and this text file is easily accessible. The path where this file is stored can be found by executing the following command:

1
(Get-PSReadlineOption).HistorySavePath

PSReadLine

To view the list of previously executed commands you can type the following:

1
Get-Content -Path (Get-PSReadlineOption).HistorySavePath

This will output the contents of the history file to your PowerShell console. Alternatively the file can be opened by your favorite editor by using the Invoke-Item cmdlet:

1
Invoke-Item -Path (Get-PSReadlineOption).HistorySavePath

A very useful feature if you accidentally closed your console without saving your command history.

Share