PowerShell Tricks for Windows 10

Publish Date: November 30, 2015

PowerShell Tricks for Windows 10

Everyone knows the Windows 10 is now available as a free upgrade. In this section, I am going to discuss about some PowerShell tricks that you can use after upgrading your PC to Windows 10.

List the Installed Device Drivers

Device driver is a software that allows your operating system to communicate with other hardware devices, like keyboard, mouse, sound cards, graphics cards, etc. One of the major concern about Windows 10 is its driver compatibility for hardware devices. When a device driver is not compatible with the operating system, the relevant hardware is rendered unusable.

The good news is that you can Windows PowerShell to get the of all of device drivers installed by using Get-WindowsDriver cmdlet. This cmdlet displays the information about driver packages in the online or offline Windows image. Open the elevated Windows PowerShell session and run the following command:

PS D:\MyScripts> Get-WindowsDriver -Online

Published Name     : oem0.inf
Original File Name : C:\Windows\System32\DriverStore\FileRepository\prnms001.inf_amd64_4fa1863520b2418e\prnms001.inf
InBox              : False
Class Name         : Printer
Boot Critical      : False
Provider Name      : Microsoft
Date               : 21-06-2006 12:00:00 AM
Version            : 6.3.9600.16384

Published Name     : oem1.inf
Original File Name : C:\Windows\System32\DriverStore\FileRepository\e1c64x64.inf_amd64_7a9a6fe10169f82f\e1c64x64.inf
InBox              : False
Class Name         : Net
Boot Critical      : False
Provider Name      : Intel
Date               : 21-08-2013 12:00:00 AM
Version            : 12.10.28.0

Published Name     : oem10.inf
Original File Name : C:\Windows\System32\DriverStore\FileRepository\klim6.inf_amd64_80872328fa400351\klim6.inf
InBox              : False
Class Name         : NetService
Boot Critical      : False
Provider Name      : Kaspersky Lab
Date               : 20-04-2015 12:00:00 AM
Version            : 8.0.0.94

[output cut]

By default, only third-party drivers are listed by this command. If you want to include default drivers from Microsoft as well, you can add -All parameter to the above command.

PS D:\MyScripts> Get-WindowsDriver -Online -All

Published Name     : 1394.inf
Original File Name : C:\Windows\System32\DriverStore\FileRepository\1394.inf_amd64_ab3552a66c5b80fd\1394.inf
InBox              : True
Class Name         : 1394
Boot Critical      : True
Provider Name      : Microsoft
Date               : 21-06-2006 12:00:00 AM
Version            : 6.3.9600.16384

Published Name     : 3ware.inf
Original File Name : C:\Windows\System32\DriverStore\FileRepository\3ware.inf_amd64_408ceed6ec8ab6cd\3ware.inf
InBox              : True
Class Name         : SCSIAdapter
Boot Critical      : True
Provider Name      : LSI
Date               : 11-04-2013 12:00:00 AM
Version            : 5.1.0.51

[output cut]

List and Uninstall Pre-Installed Apps

Like Windows 8, Windows 10 also comes up with a lot of pre-installed apps that can’t be removed by normal users because the uninstall button is grayed out. But you can still get around this issue with the help of Windows PowerShell. Run the Windows PowerShell session with administrator privilege and use Get-AppxPackage cmdlet to list all the apps installed.

PS D:\MyScripts> Get-AppxPackage

Name              : Microsoft.VCLibs.120.00
Publisher         : CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
Architecture      : X86
ResourceId        :
Version           : 12.0.21005.1
PackageFullName   : Microsoft.VCLibs.120.00_12.0.21005.1_x86__8wekyb3d8bbwe
InstallLocation   : C:\Program Files\WindowsApps\Microsoft.VCLibs.120.00_12.0.21005.1_x86__8wekyb3d8bbwe
IsFramework       : True
PackageFamilyName : Microsoft.VCLibs.120.00_8wekyb3d8bbwe
PublisherId       : 8wekyb3d8bbwe
IsResourcePackage : False
IsBundle          : False
IsDevelopmentMode : False

Name              : Microsoft.VCLibs.120.00
Publisher         : CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US
Architecture      : X64
ResourceId        :
Version           : 12.0.21005.1
PackageFullName   : Microsoft.VCLibs.120.00_12.0.21005.1_x64__8wekyb3d8bbwe
InstallLocation   : C:\Program Files\WindowsApps\Microsoft.VCLibs.120.00_12.0.21005.1_x64__8wekyb3d8bbwe
IsFramework       : True
PackageFamilyName : Microsoft.VCLibs.120.00_8wekyb3d8bbwe
PublisherId       : 8wekyb3d8bbwe
IsResourcePackage : False
IsBundle          : False
IsDevelopmentMode : False

[output cut]

By default this command shows all apps installed under your user profile. To get the list of all apps installed across all users, you can include -AllUsers parameter. To make the output further neat, you can use Format-List cmdlet to only list Name and PackageFullName properties as shown below:

PS D:\MyScripts> Get-AppxPackage | Format-List -Property Name, PackageFullName

Name            : Microsoft.VCLibs.120.00
PackageFullName : Microsoft.VCLibs.120.00_12.0.21005.1_x86__8wekyb3d8bbwe

Name            : Microsoft.VCLibs.120.00
PackageFullName : Microsoft.VCLibs.120.00_12.0.21005.1_x64__8wekyb3d8bbwe

Name            : Microsoft.WindowsAlarms
PackageFullName : Microsoft.WindowsAlarms_6.3.9654.20335_x64__8wekyb3d8bbwe

Name            : Microsoft.WindowsCalculator
PackageFullName : Microsoft.WindowsCalculator_6.3.9600.20278_x64__8wekyb3d8bbwe

Name            : Microsoft.WindowsSoundRecorder
PackageFullName : Microsoft.WindowsSoundRecorder_6.3.9600.20280_x64__8wekyb3d8bbwe

Name            : Microsoft.XboxLIVEGames
PackageFullName : Microsoft.XboxLIVEGames_2.0.139.0_x64__8wekyb3d8bbwe

[output cut]

Note the FullPackageName of all the apps you want to remove. To remove the app, use Remove-AppxPackage <PackageFullName> to uninstall.

Remove-AppxPackage Example

Launch Malware Scan

If you do not have any professional anti-virus installed in your PC, you can use inbuilt Windows Defender. I am not saying that Windows Defender is the best, but it gets the job done sometimes. Start the Windows PowerShell session with administrator privilege and use the Start-MpScan cmdlet. The cmdlet support some optional parameters like -ScanType with values QuickScan|FullScan|CustomScan and -ScanPath to provide path of directory to be scanned (C:, D:).

PS D:\MyScripts> Start-MpScan -ScanType QuickScan -ScanPath D:

Manage Windows Clipboard

Windows 10 comes up with Windows PowerShell version 5.0 which adds some cool cmdlets like Get-Clipboard and Set-Clipboard. You can use these cmdlets to view or set the clipboard contents.

PS D:\MyScripts> Get-Clipboard
Sample Text Copied
PS C:\> "Replacement Text" | Set-Clipboard
PS C:\> Get-Clipboard
Replacement Text

Bulk Rename

Sometimes you may need to rename multiple files at once. For instance, take a look at the following image:

backup files
I have some backup files placed in D:\Backup directory. I want to append the date (when the backup files were generated) at the end of filename without changing file extension. Take a look at Date Modified column marked red, all files have different timestamp.

To rename these files launch the PowerShell console, navigate to D:\Backup directory and Run the following command:

Get-ChildItem | Rename-Item –NewName { $_.BaseName + "_" + $_.LastWriteTime.ToString('ddMMyyyy') + $_.extension }

You will see that all files get renamed exactly the way I needed:

Bulk Rename Script

Another Approach

In previous example we saw how to rename files and include their actual creation date in filename. But what if you want to rename all the files and append a custom date to filename? Run the following command:

Get-ChildItem | Rename-Item –NewName { $_.BaseName + "_" + "03112017" + $_.extension }

In above command, 03112017 is today’s date (while article is written) but it can be any text string which will be appended in filename as is.

Bulk Rename

Gracefully Close Microsoft Outlook

Microsoft Outlook is very popular email client used by many organizations. If your organization uses Outlook, you must be aware of backing up PST (Personal Storage) or OST (Offline Storage) files created by Outlook. While Outlook is running, these files get locked and you cannot simply copy these files. You must close the Outlook before backing-up these files no matter which method you use for backup. You might think of using taskkill command but this can be dangerous. Using Taskkill to close Outlook can corrupt the data file which can cause a lot of trouble.

The best thing is that nowadays every version of Windows comes with PowerShell pre-installed. So, you can create a single-line PowerShell script. Following is the command:

Get-Process Outlook -ErrorAction 0 | Where-Object {$_.CloseMainWindow() | Out-Null}

The benefit of using this method is that it does not end the process forcefully; instead it gracefully exits the Outlook window. The other benefit of using this method is that if any user is writing an email message, he/she will be prompted with 3 options:YES, NO, and CANCEL.

  1. If user selects YES, the message will be saved to Drafts and Outlook will be Closed.
  2. If user selects NO, the message will not be saved to Drafts and Outlook will be Closed.
  3. If user selects CANCEL, the user can continue writing email message and Outlook will not be Closed.

You can use the same command to gracefully close any windows process without having to terminate it forcefully and losing the data.

To learn more about managing Windows processes using PowerShell, read this article.

Back



Microsoft Certified | Cisco Certified