PowerShell/docs/edit.md

96 lines
2.1 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *edit.ps1*
========================
2021-11-08 21:36:42 +01:00
2024-03-27 17:36:59 +01:00
This PowerShell script opens a text editor with the given text file.
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2024-03-27 17:36:59 +01:00
PS> ./edit.ps1 [[-path] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2024-03-27 17:36:59 +01:00
-path <String>
Specifies the path to the text file (will be queried if none given)
2021-11-08 21:36:42 +01:00
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.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
2024-03-27 17:36:59 +01:00
PS> ./edit.ps1 C:\MyDiary.txt
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
2024-03-27 17:36:59 +01:00
Opens a text editor
2022-11-17 20:02:26 +01:00
.DESCRIPTION
2024-03-27 17:36:59 +01:00
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)
2022-11-17 20:02:26 +01:00
.EXAMPLE
2024-03-27 17:36:59 +01:00
PS> ./edit.ps1 C:\MyDiary.txt
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-03-27 17:36:59 +01:00
param([string]$path = "")
2022-11-17 20:02:26 +01:00
2024-03-27 17:36:59 +01:00
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
2022-11-17 20:02:26 +01:00
}
2024-03-27 17:36:59 +01:00
}
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."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-05-19 10:25:56 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of edit.ps1 as of 05/19/2024 10:25:20)*