Skip to content

Latest commit

 

History

History
95 lines (79 loc) · 2.15 KB

edit.md

File metadata and controls

95 lines (79 loc) · 2.15 KB

Script: edit.ps1

This PowerShell script opens a text editor with the given text file.

Parameters

PS> ./edit.ps1 [[-path] <String>] [<CommonParameters>]

-path <String>
    Specifies the path to the text file (will be queried if none given)
    
    Required?                    false
    Position?                    1
    Default value                
    Accept pipeline input?       false
    Accept wildcard characters?  false

[<CommonParameters>]
    This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, 
    WarningVariable, OutBuffer, PipelineVariable, and OutVariable.

Example

PS> ./edit.ps1 C:\MyDiary.txt

Notes

Author: Markus Fleschutz | License: CC0

Related Links

https://github.com/fleschutz/PowerShell

Script Content

<#
.SYNOPSIS
	Opens a text editor
.DESCRIPTION
	This PowerShell script opens a text editor with the given text file.
.PARAMETER path
	Specifies the path to the text file (will be queried if none given)
.EXAMPLE
	PS> ./edit.ps1 C:\MyDiary.txt
.LINK
	https://github.com/fleschutz/PowerShell
.NOTES
	Author: Markus Fleschutz | License: CC0
#>

param([string]$path = "")

function TryEditor { param([string]$editor, [string]$path)
	try {
		Write-Host -noNewline "$editor·"
		& $editor "$path"
		if ($lastExitCode -ne "0") {
			"⚠️ Can't execute '$editor' - make sure it's installed and available"
			exit 1
		}
		exit 0 # success
	} catch {
		return
	}
}

try {
	if ($path -eq "" ) { $path = Read-Host "Enter the path to the text file" }

	Write-Host -noNewline "Trying "
	TryEditor "vim" $path
	TryEditor "vi" $path
	TryEditor "nano" $path
	TryEditor "pico" $path
	TryEditor "emacs" $path
	TryEditor "notepad.exe" $path
	TryEditor "wordpad.exe" $path
	Write-Host ""

	throw "No text editor found - use 'winget install' to install your favorite text editor."
	exit 0 # success
} catch {
	"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
	exit 1
}

(generated by convert-ps2md.ps1 using the comment-based help of edit.ps1 as of 05/19/2024 10:25:20)