PS script to open WinExplorer with a file selected

I’m trying to code a PS script to open WinExplorer with a file selected. The following is the code I am trying to run under PS 7 (“C:\Program Files\PowerShell\7\pwsh.exe”):

$path = "E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk"
explorer /select, $path

I don’t know what I am doing wrong. Any suggestions would be much appreciated.

Interesting, I didn’t know this was a thing. You simply need to remove the space after the comma

$path = "E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk"
explorer /select,$path
1 Like

That’s really cool. I didn’t know you could do that either. Inspired this function:

function Start-Explorer {
    [CmdletBinding()]
    param (
        [Parameter()]
        [ValidateScript({Test-Path $_})]
        [System.IO.FileInfo]$Path
    )
    if ($Path.Attributes -eq 'Directory') {
        $Path = Get-ChildItem -Path $Path.FullName -File | Select-Object -First 1
    }
    Write-Verbose "Opening Explorer to $($Path.FullName)"
    Start-Process -FilePath explorer.exe -ArgumentList "/select,$($Path.FullName)"
}
1 Like

Thank you. Your insight made it work under Windows PS.
But when I run the script under PS7,
"C:\Program Files\PowerShell\7\pwsh.exe" "E:\Apps\UtilPS\Open_WinExplorer_and_select_a_file_aac.ps1"
it opens “This PC”.

i’ve run in to this before in Windows Powershell when using the System.IO.FileInfo class for a parameter. If given a relative path, e.g. “.\SomeFile.txt”, it can lead to Powershell assuming that file is in your home directory, where it isn’t.
I copied my code in to @psdarwin function for dealing with that scenario and it worked on my machine in PS 7

function Start-Explorer {
    [CmdletBinding()]
    param (
        [Parameter()]
        [ValidateScript({Test-Path $_})]
        [System.IO.FileInfo]$Path
    )
    if (-not $Path.Exists) {
        # this means we were given a relative path. Resolve it to its full path
        $ResolvedPath = Resolve-Path -Path $Path | Select-Object -Expandproperty ProviderPath
        if ($ResolvedPath) {
            [System.IO.FileInfo]$Path = $ResolvedPath
        }
    }
    if ($Path.Attributes -eq 'Directory') {
        $Path = Get-ChildItem -Path $Path.FullName -File | Select-Object -First 1
    }
    Write-Verbose "Opening Explorer to $($Path.FullName)"
    Start-Process -FilePath explorer.exe -ArgumentList "/select,$($Path.FullName)"
}

Thank you so much.
I simplified the code when I posted it, replacing the command line parameter:

param (
    [string]$arg_fullN_01='default'
)

with the statement: $path = “E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk”

I’m telling you this because I’m too ignorant to understand how to use the parameters for your function:

param (
        [Parameter()]
        [ValidateScript({Test-Path $_})]
        [System.IO.FileInfo]$Path
    )

In my param statement above, there is only one bracketed thing, [string].
Your param statement has three bracketed things:
I don’t understand the role of [Parameter()] or the role of [ValidateScript({Test-Path $_})].
I presume that [System.IO.FileInfo]$Path is a way of checking that a valid path was used as an argument. Am I right?

Could you direct me to a URL where I could learn how to feed the right strings to your function?

To show you what I mean by that question, the original script, as improved according to the helpful input from @krzydoug, runs just fine under Windows PS, when I run the following Terminal command:
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" E:\Apps\UtilPS\powershell_make_shortcut_using_powershell_SAVE_aaa.ps1" "E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk"

But when I run the script under PS7, using the following command line, it opens “This PC”
"C:\Program Files\PowerShell\7\pwsh.exe" "E:\Apps\UtilPS\powershell_make_shortcut_using_powershell_SAVE_aaa.ps1" "E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk"

In the terminal command above, which runs the script under PS7, the path, I feed the path-string "E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk" to the param statement:

param (
    [string]$arg_fullN_01='default'
)

Assuming I remove from the script the statement:
$path = "E:\Apps\UtilPS\deleete.pdf (Shortcut).lnk",
how should I modify the script AND the command line, to use your function?

Any suggestions about where I could learn what I am missing would be much appreciated.

Thank you again. I really do appreciate your generosity and @krzydoug’s generosity in helping me.
Marc

function Start-Explorer {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [ValidateScript({Test-Path $_})]
        [System.IO.FileInfo]$Path
    )
    if (-not $Path.Exists) {
        Write-Verbose 'Resolving relative path to absolute path.'
        $ResolvedPath = Resolve-Path -Path $Path | Select-Object -Expandproperty ProviderPath
        if ($ResolvedPath) {
            [System.IO.FileInfo]$Path = $ResolvedPath
        }
    }
    if ($Path.Attributes -eq 'Directory') {
        Write-Verbose 'Path is a directory. Resolving a file in the directory.'
        $Path = Get-ChildItem -Path $Path.FullName -File | Select-Object -First 1
    }
    Write-Verbose "Opening Explorer to $($Path.FullName)"
    Start-Process -FilePath explorer.exe -ArgumentList "/select,$($Path.FullName)"
}

Slightly tweaked to help explain.
[Parameter(Mandatory)]: this allows you to set properties of your parameter. In this case, I set it to Mandatory.
[ValidateScript({Test-Path $_})]: this validates that the path is a valid path.
[System.IO.FileInfo]$Path: this forces the input to be in the format of a fileinfo object (valid path), making non-path strings invalid.

Example of usage of this function:
Start-Explorer -Path c:\temp\file1.txt

If you are running this as a script instead of interactively, that line goes at the end of your ps1 file, after the function declaration. So the code block above declares the function, and the command at the end actually runs the function with that parameter.

I hope that helps explain.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.