EB farnum
EB farnum

Reputation: 103

How to open 'This PC' using CMD or Powershell?

I need a way of opening the explorer at This PC , in the same way as opening in a windows explorer Gui, but I am incapable of this as using Explorer.exe in CMD opens quick access.

I tried opening it with a shortcut and that too failed.

Does anyone know of a way to do this?

Cheers

Upvotes: 10

Views: 29392

Answers (7)

Ilya Serbis
Ilya Serbis

Reputation: 22283

explorer shell:MyComputerFolder

Other folders are available as well (the entire list is available at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\explorer\FolderDescriptions, see Name values)

Reference: https://www.winhelponline.com/blog/shell-commands-to-access-the-special-folders/

Credit: https://stackoverflow.com/a/63436479/355438

Upvotes: 1

hiyorijl
hiyorijl

Reputation: 66

All of this comment uses Powershell 7, idk if it works on Windows Powershell 5.1. and ofc it's using Windows Terminal:

Some background info

For documentation purposes mainly, you can skip to the next Header (-->next h2)

$shell = New-Object -ComObject Shell.Application
$thisPCFolder = $shell.Namespace(0x11)

(0x11) is same as 20D04FE0-3AEA-1069-A2D8-08002B30309D

so the second line is the same as

$thisPCFolder = $shell.NameSpace('::{20D04FE0-3AEA-1069-A2D8-08002B30309D}')

use whichever.

Take into account that This PC isn't an actual folder, it's a virtual one, so for that you have to use Shell.Application COM objects.

The Shell.Application COM object uses these numeric representations to access specific special folders and namespaces in the Windows Shell. Which is the first block code.

So 0x11 isn't the only one who works these constants Windows Shell API and can be found in the Shlobj.h header file, which is a part of the Windows Software Development Kit (SDK). Like these constants:

  • 0x00 - Desktop
  • 0x02 - Programs
  • 0x03 - Control Panel
  • 0x04 - Printers
  • 0x05 - My Documents (Documents)
  • 0x06 - Favorites
  • 0x07 - Startup Folder
  • 0x08 - Recent Items
  • 0x09 - Send To
  • 0x0a - Recycle Bin
  • 0x0b - Start Menu
  • 0x0d - My Music
  • 0x0e - My Videos
  • 0x0f - Desktop (for a specific user)
  • 0x10 - Control Panel (virtual folder)
  • 0x11 - This PC (virtual folder)
  • 0x12 - Network
  • 0x13 - Fonts
  • 0x14 - My Computer
  • 0x15 - My Network Places

So you don't have to download any module or anything, you just need to have Powershell 7 and you should be good to execute the code on your machine.

So to enumerate the disks I did this:

# Create a new instance of Shell.Application
$shell = New-Object -ComObject Shell.Application

# Get the "This PC" folder
$thisPCFolder = $shell.Namespace(0x11)

# Enumerate through the items in "This PC" folder
foreach ($item in $thisPCFolder.Items()) {
    # Display the name and path of each item
    $name = $thisPCFolder.GetDetailsOf($item, 0)
    $path = $thisPCFolder.GetDetailsOf($item, 217)
    Write-Output "$name : $path"
}

printing this on my Terminal when I execute it:

Local Disk (C:) :
name_of_your_disk_D (D:) :

you can skip this As an example of usage on real code application you can read this and see how 0x11 is used github code file on line 40 currently ........

Or you can just use Start-Process C:\ or Start-Process D:\ if you know already the name of your disk and then press Alt + ↑

The real tutorial

But the answer you might really want is this, copy paste this block code on your PowerShell and see what happens:

# Use the Start-Process cmdlet to open "This PC" (My Computer) in Windows Explorer
Start-Process "explorer.exe" "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"

Now you could use an Alias for this last block code and then put it on $PROFILE so it applies to your shell each time you open it and not only one session. This how to do so:

  • First corroborate if you have a $PROFILE:
Test-Path $PROFILE
  • If False you need to create a file, run:
New-Item -ItemType File -Path $PROFILE -Force
  • Now you need to open the notepad to insert the code:
notepad.exe $PROFILE
  • Copy paste this on the last line if you already have some code
function MyPC {
    Start-Process "explorer.exe" "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
}
  • Restart you PowerShell 7 terminal, reload it using this command if you don't want to close your Terminal and open it again:
. $PROFILE

Now each time you use MyPC alias in your PowerShell 7 terminal it will open This PC in a new Windows Explorer window.

edit: so i just read the above answer (the most voted one on here) so basically you could just go to Folder options and change from Home to This PC and then just write explorer on your PowerShell and it will lead you to This PC.

sources: https://www.elevenforum.com/t/list-of-windows-11-clsid-key-guid-shortcuts.1075/

Upvotes: 1

LKL
LKL

Reputation: 31

Open the Command Prompt and type this in:

explorer =

Upvotes: 3

codewario
codewario

Reputation: 21418

Just like you can run explorer.exe C:\ to open up the C: (or any other drive or folder path), you can also use a file: URI to open paths in Explorer as well. If you just specify the file: protocol without a directory (or specify file:\\), it will open This PC:

explorer file:
explorer file:\\

Note that you can also change the default Explorer location from Quick Access to This PC as well from Folder Options:

Folder Options

You can set this in the registry too if you're after automation to configure this. See my answer on how you can use PowerShell to set the default launch folder in the registry.

Upvotes: 19

jacouh
jacouh

Reputation: 8741

On Powershell, we can do this:

(New-Object -ComObject Shell.Application).Namespace("").Self.InvokeVerb()

We create a com object of class Shell.Application, then call .InvokeVerb() that invokes the default verb "open".

Or more rigourously, by defining ssfDRIVES constant (see Microsoft reference)

Set-Variable ssfDRIVES -Option Constant -Value 0x11
(New-Object -ComObject Shell.Application).Namespace($ssfDRIVES).Self.InvokeVerb()

ssfDRIVES 0x11 (17). Namespace points to My Computer—the virtual folder that contains everything on the local computer: storage devices, printers, and Control Panel. This folder can also contain mapped network drives.

Upvotes: 2

wasif
wasif

Reputation: 15478

You can use this in cmd:

Explorer /root,

Upvotes: 2

Compo
Compo

Reputation: 38613

My experience is that, the following should work:

From :

Start "" "%SystemRoot%\explorer.exe" /Select,"This PC"

From :

Start "$Env:SystemRoot\explorer.exe" "/Select,'This PC'"

Upvotes: 4

Related Questions