3. May 2021

How to add/remove device driver from command-line on Windows

Windows operating system contains a tool that allows manipulation with system drivers stored in .INF and .CAT files. The operation requires Administrator privileges.

Command to install the driver:

pnputil /add-driver my-driver.inf /install

The correct installation display the following result:

Microsoft PnP Utility

Adding driver package:  my-driver.inf
Driver package added successfully.
Published Name:         oem3.inf

Total driver packages:  1
Added driver packages:  1

To verify the result you can plug the device and it should pick the correct driver. The system copied content from my-driver.inf to C:\Windows\INF\oem3.inf. The number in file name might be different.

The remove the driver from the system and perform uninstallation of the driver from devices type following command:

pnputil /remove-driver my-driver.inf /uninstall

Expected result:

Microsoft PnP Utility

Driver package deleted successfully.

27. April 2021

Quick way to clear all Windows Defender exclusions

Windows Defender has support for a list of exclusions that won’t be considered for scanning. The UI for manipulating with exclusions is quite simple and does not allow operations on multiple exclusions at once. Fortunately, there are PowerShell commands which can help.

These commands are Add-MpPreference, Get-MpPreference, and Remove-MpPreference.

Following commands allows to quickly remove all exclusions of Path. The command must be started in Administrator’s shell:

$Paths=(Get-MpPreference).ExclusionPath
foreach ($Path in $Paths) { Remove-MpPreference -ExclusionPath $Path }

A similar principle can be applied on the rest of the exclusions: ExclusionExtension, ExclusionIpAddress, ExclusionProcess

$Extensions=(Get-MpPreference).ExclusionExtension
foreach ($Extension in $Extensions) { Remove-MpPreference -ExclusionExtension $Extension }

$Processes=(Get-MpPreference).ExclusionProcess
foreach ($Process in $Processes) { Remove-MpPreference -ExclusionProcess $Process }

20. April 2021

How to statically link Rust application for Windows

Rust compiler can generate a single binary that contains all dependencies. This is a great feature for creating stand-alone tools or tools for containers.

There is one gotcha for Windows which does not appear on a developer’s machine. When you move the application to a brand new installation of Windows or you try to start the in Windows Docker Container which contains Windows for Datacenters the app won’t start.

Why? The system is missing Microsoft Visual C++ Redistributable 2015-2019 (vc_redist) which you can download from microsoft.com.

It’s quite inconvenient to force the user to install the package in the case of a stand-alone Rust tool. The package installation even requires elevation of privileges.

The alternative approach to distributing vc_redist is to statically link CRT library into the application. It will result in a slightly bigger application around +100KB.

It’s necessary to tell rustc to perform static linking of CRT.

In the project create file .cargo/config.toml:

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

Rebuild the application and the new binary is statically linked and can run even on Windows 8 without vc_redist.

The interesting part is how to determine the proper target name which is on the first line of the example configuration. Many examples on the internet are referring to target.i686-pc-windows-msvc which does not work. Use the following command to determine parts of the name of the target:

rustc --print cfg
target_arch="x86_64"
target_endian="little"
target_env="msvc"
target_family="windows"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="windows"
target_pointer_width="64"
target_vendor="pc"
windows

The target name is composed of values on the lines above.

1. March 2021

Lenovo T14 CPU locked at 0.8 GHz on Windows

Users with brand new Lenovo T14 might face strange behavior on Windows. The computer seems to be slow and the detail of CPU usage displays a constant CPU speed set to 0.8 GHz.

The problem can be solved by upgrading BIOS. Drivers can be found at pcsupport.lenovo.com

1. December 2020

How to switch tabs in Windows Terminal

Windows Terminal is very versatile shell for Windows which allows to run CMD, PowerShell or Linux in WSL2. The terminal also supports tabs.

To switch between tabs you can use:

  • CTRL+Tab – switch to next tab
  • CTRL+Shift+Tab – switch to previous tab
  • If you’d like to use own keyboard shortcuts like CTRL+PgUp, CTRL+PgDown, then just open Settings CTRL+, and enter following code into section actions:

    ..."actions":  [
     ...
      { "command": "nextTab", "keys": "ctrl+pgdn" },
      { "command": "prevTab", "keys": "ctrl+pgup" }
     ...
    ]