【问题标题】:Create a Clickable Folder Link in Powershell在 Powershell 中创建可点击的文件夹链接
【发布时间】:2021-08-06 19:11:30
【问题描述】:

我有一个在文件夹中搜索文件名并给出文件夹路径的脚本。 Powershell 是否有办法使这些文件夹路径成为可点击的链接?我想调出一个可以点击的文件目录路径列表。

这是代码的样子。

#Declare Variables. 
$Software = @()

#Delcares Directories to search. 
$Directories = @(
    '\\Software\Unlicensed Software'
    '\\Software\Licensed Software')

#Gets folder contents of directories at a depth of three. 
Foreach($Directory in $Directories)
{ 
    $Path = Get-ChildItem $Directory -Recurse -Depth 3| ?{ $_.PSIsContainer } | Select-Object FullName;
    $Software += $Path
}

#Gets user string. 
$target = Read-Host -Prompt 'Input a software name or part of a name. ("Exit" to quit)';

#Finds matches and adds them to Links. 
while ($target -ne "exit")
{
   $count = 0;
   $Links = New-Object Collections.Generic.List[string];
    Foreach ($line in $Software)
    {   
        if($line -like "*$target*")
            {
                $Links.Add($line.FullName);
                $count += 1;
            }

#Stops code when results are greater than 100 entries. 
        if($count -gt 99)
            {
                Write-Output "Your search result yielded more than 100 entries. Try narrowing down your search."
                Break
            }
    }

#Prints links. 
    ForEach($Link in $Links)
    {
        Write-Output $Link;
    }
   Write-Host `n$count" entries found`n";

#Asks users if they would like to continue. 
   $target = Read-Host -Prompt 'Input a software name or part of a name ("Exit" to quit)';
}

#Exits Program
Write-Host "Press any key to continue ...";
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");

【问题讨论】:

  • 使用Out-GridView -PassThru 可能是一种简单的方法,但您需要与我们分享您的代码示例。
  • 示例代码。假设我有一个名为的文件夹路径:$FilePath ='\\Software\Licensed Software" 我想单击一个名为 FilePath 的链接并转到文件夹路径。

标签: powershell directory hyperlink clickable


【解决方案1】:

这是一种非常简单的方法,您可以使用它来选择文件夹并打开它,它会显示Out-GridView,直到您取消或关闭网格并在您做出选择后打开文件夹。如果您正在寻找更好看和更复杂的东西,则必须编写自己的 WinForm 应用程序。

$directories = Get-ChildItem $env:USERPROFILE -Directory |
               Select-Object Name, FullName

do
{
    $selection = $directories |
    Out-GridView -PassThru -Title 'Choose a Folder'

    if($selection){ Invoke-Item $selection.FullName }

} while($selection)

【讨论】:

    【解决方案2】:

    你可以制作一个 HTML 文件:

    Add-Type -AssemblyName System.Web
    [System.Web.HttpUtility]::HtmlDecode((gci | select Name,@{n='Fullpath'; e={(("<a href='file://" +$_.fullname+"'>"+$_.Fullname+'</a>' ))}} | ConvertTo-Html)) | out-file c:\file.html
    iex  c:\file.html
    

    【讨论】:

      猜你喜欢
      • 2016-07-31
      • 2019-07-19
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2020-06-28
      • 2012-01-24
      相关资源
      最近更新 更多