Skip to content

Instantly share code, notes, and snippets.

@LuanRoger
Created February 22, 2022 19:41
Show Gist options
  • Save LuanRoger/97da310fc1bda4a30f3db2df10b9080a to your computer and use it in GitHub Desktop.
Save LuanRoger/97da310fc1bda4a30f3db2df10b9080a to your computer and use it in GitHub Desktop.
Create notes from the terminal.
<#
.SYNOPSIS
Create notes from the terminal.
.DESCRIPTION
Create notes from the terminal and save the most important commands.
.PARAMETER Note
Note to save
.PARAMETER Path
File path to save the file (whitout the file name)
.PARAMETER FileName
Name of the file, this will be used to append notes if exist
.PARAMETER ReadNotes
Read notes in the file
.PARAMETER Help
Show instructions about how to use the script
.PARAMETER Info
Show info about the script
.EXAMPLE
PS> power_note.ps1 "git status"
.EXAMPLE
PS> power_note.ps1 -ReadNotes
#>
param (
[Parameter(Position=0)][string]$Note,
[Parameter(Mandatory=$false)][string]$Path = $PSScriptRoot,
[Parameter(Mandatory=$false)][string]$FileName = "notes.txt",
[Parameter(Mandatory=$false)][switch]$ReadNotes = $false,
[Parameter(Mandatory=$false)][switch]$Help = $false,
[Parameter(Mandatory=$false)][switch]$Info = $false
)
#Variables
$FilePath = "$Path\$FileName"
function WriteText {
<#
.SYNOPSIS
Create or append text in a file.
.DESCRIPTION
Create or append text in a file.
If the file not exist, will be created and write the note and return true.
Else will be append and return false.
#>
param (
[string]$Text,
[string]$FilePath
)
if(Test-Path -Path $FilePath -PathType Leaf) {
Add-Content -Path $FilePath -Value $Text
return $false
}
Set-Content -Path $FilePath -Value $Text
return $true
}
function ReadNotes {
param (
[string]$FilePath
)
Get-Content -Path $FilePath | Write-Host
exit
}
function ScriptHelp {
Get-Help $PSCommandPath -Detailed
exit
}
function ShowInfo {
Write-Host "Power Note Script"
Write-Host "Follow me on GitHub: https://github.com/LuanRoger"
Write-Host "Script version: v1.0"
exit
}
if($Help) {
ScriptHelp
}
if($Info) {
ShowInfo
}
if($ReadNotes) {
ReadNotes $FilePath
}
if([string]::IsNullOrEmpty($Note)) {
Write-Error "The note can't be empty"
exit
}
if(WriteText $Note $FilePath) {
Write-Host "A new file has been created in $FilePath"
}
else {
Write-Host "The text has been written in $FilePath"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment