Open a file from an HTML file created in PowerShell

I have a script in PowerShell that creates an HTML file with the list of files of specific types (e.g. ".txt") with some basic information about them. Now I wanted to add a feature that one could click the filename in the HTML page created by the script in order to open it. Could anyone at least give a hint on how to do it? This is the current output I receive (I blurred the text though):

Here is the script I'm using (from alroc's answer to this question: Write multiple objects obtained via get-ChildItem into HTML):

$types=@("*.txt", "*.pdf")
$myfiles = @();
foreach($type in $types){
    $myFiles += Get-ChildItem C:\POWERSHELL -Filter $type -Recurse 

$myfiles | ConvertTo-Html -property fullname,lastwritetime,length | out-file c:\status23.html;


In order to make a clickable link to the file, you need to use this kind of HTML tag:

<a href='file:///filename'>Display Text</a>`

However, if you want to change the HTML, you can't use ConvertTo-Html as in the script you're referencing, because that cmdlet will escape any input you provide it, so you need to create the HTML yourself. For a simple table, it's not that hard, and it lets you have more control over what the output will look like.

Try this (I've compacted your code in addition to adding the HTML output):

$output = "<html>`n<table>`n<tr><th align='left'>Full Name</th><th align='left'>Last Write Time</th><th align='right'>Size (bytes)</th></tr>`n"
Get-ChildItem '*.txt','*.ps1' -Recurse | %{
  $output += "<tr><td><a href='file:///$($_.FullName)'>$($_.FullName)</a></td><td>$($_.LastWriteTime)</td><td align='right'>$($_.Length)</td></tr>`n"
$output += "</table>`n</html>"
$output | Out-File status23.html

A few notes:

  • You don't have to worry about HTML-escaping the interpolated values, because none of the properties you're including (FullName, LastWriteTime, Length) can contain any characters that need to be escaped in HTML.
  • The `ns aren't strictly necessary, they just make the resultant HTML file more readable; the page would be rendered the same if you left them out.

To start, you need to wrap them in an html href tag with a path to the file.

If they have access to that txt file, and they are using a browser to view the html file, the text file will open within the browser when clicked.



"<HTML><a href=`"$file`">$file</a></HTML>" | Out-file "C:\test\test.html"

Need Your Help

The key 'CategoryID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

c# dropdownlistfor

Got a little problem with saving my Product. Dropdownlist is getting populated, abut when I try to save I have an error which is stated in title. I read tons of articles about it, but any of it hel...

Unexpected Flash Security Exception When Using URLLoader

actionscript-3 file-upload securityexception urlrequest urlloader

What I am trying to accomplish is to upload some binary data, specifically a ByteArray representing a PNG image, to a server using the URLLoader class in conjunction with URLRequest.