commit f0805aa6a6a6d3135740de6279b5d23572ee7ae2 Author: Tomasz Kostrzewa Date: Wed May 1 14:12:54 2024 +0200 commit message diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..449268f --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024 najlepszytomasz + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..19fe8f3 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# PowerShell-Profile + diff --git a/RdzeN-Profile.ps1 b/RdzeN-Profile.ps1 new file mode 100644 index 0000000..b70c425 --- /dev/null +++ b/RdzeN-Profile.ps1 @@ -0,0 +1,114 @@ +# gitea.RdzeN.net +# +# Example Powershell Profile. +# + +# TO-DO +# Step 0, precheck for git, notepad++ and vscode installed. + +# 1. Check if GITEA folder exists. +$path_gitea = "$($env:USERPROFILE)\GITEA" +$path_gitea_powershell_profile = "$path_gitea\PowerShell-Profile" +$path_gitea_powershell_profile_functions = "$path_gitea_powershell_profile\functions" + +if (-not (Test-Path $path_gitea)) { + New-Item -ItemType Directory -Path $path_gitea | Out-Null +} +if (-not (Test-Path $path_gitea_powershell_profile)) { + New-Item -ItemType Directory -Path $path_gitea_powershell_profile | Out-Null +} +if (-not (Test-Path $path_gitea_powershell_profile_functions)) { + New-Item -ItemType Directory -Path $path_gitea_powershell_profile_functions | Out-Null +} + +# 2. Check if local GIT repo is up to date +Set-Location $path_gitea_powershell_profile +$RepositoryURL = "https://gitea.rdzen.net/najlepszytomasz/PowerShell-Profile.git" + +try { + $git_tmp = git fetch origin + Write-Host "Success + git fetch origin + : $RepositoryURL" -ForegroundColor Green +} catch { + Write-Host "Error occurred while git fetch origin + : $RepositoryURL" -ForegroundColor Red +} +try { + $git_tmp = git status + Write-Host "Success + git status + : $RepositoryURL" -ForegroundColor Green +} catch { + Write-Host "Error occurred while git status + : $RepositoryURL" -ForegroundColor Red +} + +# 3. Download GIT REPO +function Pull-Repository { + Set-Location $path_gitea_powershell_profile + try { + git pull -q + Write-Host "Success + git pull + : $RepositoryURL" -ForegroundColor Green + } catch { + Write-Host "Error occurred while git pull for: $RepositoryURL" -ForegroundColor Red + } +} + +if ($git_tmp -match "(Your branch is up to date)") { + $git_tmp | ForEach-Object { + Write-Host $_ -ForegroundColor Green + } +} else { + $git_tmp | ForEach-Object { + Write-Host $_ -ForegroundColor Red + } + Pull-Repository +} + +# 4. Load functions +$path_to_search = "$path_gitea_powershell_profile_functions" +Get-ChildItem $path_to_search | ForEach-Object { + . $_.FullName +} + +# 5. List functions +$found_functions = @() +Get-ChildItem $path_to_search | ForEach-Object { + $file_content = Get-Content $_.FullName -Raw + $matched_functions = [Regex]::Matches($file_content, '(?<=function\s).*?(?={)') + $matched_functions | ForEach-Object { + $found_functions += $_.Value.Trim() + } +} +$found_functions = $found_functions | Sort-Object +for ($i = 0; $i -lt $found_functions.Count; $i++) { + Write-Host "$($i+1)" -NoNewline -ForegroundColor Cyan + Write-Host ")`t" -NoNewline -ForegroundColor Blue + Write-Host "$($found_functions[$i])" -ForegroundColor Cyan +} + +# 6. Check if current Profile is equal to repo profile file. +$profile_repo = Get-Content "$path_gitea_powershell_profile\RdzeN-Profile.ps1" +$profile_system = Get-Content $PROFILE +$compare_profile = Compare-Object -ReferenceObject $profile_repo -DifferenceObject $profile_system + +# 7. Override actual profile and reload it if needed. +if ($compare_profile.sideindicator.count -gt 0) { + Write-Host "Pulled profile file need to be updated" -ForegroundColor Red + clear-host + Override-Profile + Write-Host "Profile updated" -ForegroundColor Red + Reload-Profile +} else { + # Just override ;-) + Override-Profile +} + +# 98. Set lcoation to userprofile folder +Set-Location $env:USERPROFILE + +# 99. Prompt profile loaded +New-SimulateTyping "PowerShell User Profile is loaded" \ No newline at end of file diff --git a/functions/Find-Notepadpp.ps1 b/functions/Find-Notepadpp.ps1 new file mode 100644 index 0000000..3522cb2 --- /dev/null +++ b/functions/Find-Notepadpp.ps1 @@ -0,0 +1,26 @@ +# gitea.RdzeN.net +# Find notepad++ +# use notepad++ as editor + +function Find-Notepadpp { + $a = Get-ChildItem -Path $env:ProgramFiles -Filter "notepad++.exe" -Recurse -File -ErrorAction SilentlyContinue + $b = Get-ChildItem -Path ${env:ProgramFiles(x86)} -Filter "notepad++.exe" -Recurse -File -ErrorAction SilentlyContinue + + if ($a -ne $null) { + $notepadpp = $a[0].FullName + } else { + $notepadpp = $b[0].FullName + } + if ($notepadpp -ne $null) { + return $notepadpp + } +} +$notepadpp = Find-Notepadpp +if ($notepadpp -ne $null) { + function EditN { + param ( + $filepath + ) + Start-Process $notepadpp -ArgumentList $filepath + } +} \ No newline at end of file diff --git a/functions/Find-VSCode.ps1 b/functions/Find-VSCode.ps1 new file mode 100644 index 0000000..00c0ce3 --- /dev/null +++ b/functions/Find-VSCode.ps1 @@ -0,0 +1,26 @@ +# gitea.RdzeN.net +# Find notepad++ +# use notepad++ as editor + +function Find-Vscode { + $a = Get-ChildItem -Path $env:ProgramFiles -Filter "Code.exe" -Recurse -File -ErrorAction SilentlyContinue + $b = Get-ChildItem -Path ${env:ProgramFiles(x86)} -Filter "Code.exe" -Recurse -File -ErrorAction SilentlyContinue + + if ($a -ne $null) { + $vscode = $a[0].FullName + } else { + $vscode = $b[0].FullName + } + if ($vscode -ne $null) { + return $vscode + } +} +$vscode = Find-Vscode +if ($vscode -ne $null) { + function EditV { + param ( + $filepath + ) + Start-Process $vscode -ArgumentList $filepath + } +} \ No newline at end of file diff --git a/functions/Get-FileSize.ps1 b/functions/Get-FileSize.ps1 new file mode 100644 index 0000000..9edf968 --- /dev/null +++ b/functions/Get-FileSize.ps1 @@ -0,0 +1,23 @@ +# gitea.RdzeN.net +# +# Get filesize. + +function Get-FileSize { + param ( + [string]$FilePath + ) + + $fileInfo = Get-Item $FilePath + $fileSizeBytes = $fileInfo.Length + + $fileSizeKB = [math]::Round($fileSizeBytes / 1KB, 2) + $fileSizeMB = [math]::Round($fileSizeBytes / 1MB, 2) + $fileSizeGB = [math]::Round($fileSizeBytes / 1GB, 2) + + return [PSCustomObject]@{ + "Bytes" = $fileSizeBytes + "KB" = $fileSizeKB + "MB" = $fileSizeMB + "GB" = $fileSizeGB + } +} diff --git a/functions/Get-ObjectCount.ps1 b/functions/Get-ObjectCount.ps1 new file mode 100644 index 0000000..fd3ed08 --- /dev/null +++ b/functions/Get-ObjectCount.ps1 @@ -0,0 +1,31 @@ +function Get-ObjectCount { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + [switch]$Recurse, + [switch]$IncludeSize + ) + + $objects = $null + + if ($Recurse) { + $objects = Get-ChildItem -Path $Path -Recurse + } else { + $objects = Get-ChildItem -Path $Path + } + + $count = $objects.Count + + if ($IncludeSize) { + $totalSizeBytes = ($objects | Measure-Object -Property Length -Sum).Sum + + $totalSizeKB = [math]::Round($totalSizeBytes / 1KB, 2) + $totalSizeMB = [math]::Round($totalSizeBytes / 1MB, 2) + $totalSizeGB = [math]::Round($totalSizeBytes / 1GB, 2) + + Write-Output "Total objects: $count, Total size: $totalSizeBytes bytes ($totalSizeKB KB, $totalSizeMB MB, $totalSizeGB GB)" + } else { + Write-Output "Total objects: $count" + } +} \ No newline at end of file diff --git a/functions/Get-Process-Detailed.ps1 b/functions/Get-Process-Detailed.ps1 new file mode 100644 index 0000000..ce5617a --- /dev/null +++ b/functions/Get-Process-Detailed.ps1 @@ -0,0 +1,40 @@ +# gitea.RdzeN.net +# +# usage: +# Get-Process-Detailed -ProcessName "notepad" +function Get-Process-Detailed { + param ( + $ProcessName="*" + ) + try { + Write-Host "Process search name: $ProcessName" -ForegroundColor Yellow + $processesCIM = Get-CimInstance -ClassName Win32_Process | Where-Object {$_.ProcessName -match "($ProcessName)"} + $totalMemoryUsed = 0 + } catch { + Write-Host "Error Occured: $($Error[0] | select *)" + } + + foreach ($process in $processesCIM) { + $memoryUsageMB = [math]::Round($process.WorkingSetSize / 1MB, 2) + $memoryUsageGB = [math]::Round($process.WorkingSetSize / 1GB, 2) + + Write-Host "Process ID: $($process.ProcessId)" + Write-Host "Process Name: $($process.Name)" + Write-Host "Path to EXEC: $($process.ExecutablePath)" + Write-Host "Command Line: $($process.CommandLine)" + Write-Host "Run Time: $($process.CreationDate)" + Write-Host "Memory Usage: $memoryUsageMB MB ($memoryUsageGB GB)" + Write-Host "Details: Get-CimInstance -ClassName Win32_Process | Where-Object {`$_`.processid -eq `"$($process.ProcessId)`"} | select *" -ForegroundColor Yellow + Write-Host "----------------------------------------" + + $totalMemoryUsed += $process.WorkingSetSize + } + + $totalPhysicalMemory = (Get-CimInstance -ClassName Win32_ComputerSystem).TotalPhysicalMemory + $totalMemoryUsedMB = [math]::Round($totalMemoryUsed / 1MB, 2) + $totalMemoryUsedGB = [math]::Round($totalMemoryUsed / 1GB, 2) + $totalMemoryUsedPercent = [math]::Round(($totalMemoryUsed / $totalPhysicalMemory) * 100, 2) + + Write-Host "Total Memory Used by listed Processes: $totalMemoryUsedMB MB ($totalMemoryUsedGB GB)" + Write-Host "Total Memory Used Percentage: $totalMemoryUsedPercent%" +} \ No newline at end of file diff --git a/functions/New-OrganiseDownloads.ps1 b/functions/New-OrganiseDownloads.ps1 new file mode 100644 index 0000000..377a9f6 --- /dev/null +++ b/functions/New-OrganiseDownloads.ps1 @@ -0,0 +1,24 @@ +# gitea.RdzeN.net +# Work with download folder - organise by date +# + +$downloadsPath = "$env:userprofile\Downloads" + +# Getting today's date and calculating yesterday's date +$today = Get-Date +$yesterday = $today.AddDays(-1).ToString("yyyy-MM-dd") + +# Path to the folder for files from yesterday +$yesterdayFolder = Join-Path -Path $downloadsPath -ChildPath $yesterday + +# If the folder for yesterday's files doesn't exist, it is created +if (-not (Test-Path -Path $yesterdayFolder)) { + New-Item -Path $yesterdayFolder -ItemType Directory | Out-Null +} + +# Getting all files from the Downloads folder, older than today +$filesToMove = Get-ChildItem -Path $downloadsPath | Where-Object {$_.LastWriteTime -lt $today -and $_.Name -notmatch '^\d{4}-\d{2}-\d{2}$' +} + +# Moving files to the folder from yesterday +$filesToMove | Move-Item -Destination $yesterdayFolder diff --git a/functions/New-SimulateTyping.ps1 b/functions/New-SimulateTyping.ps1 new file mode 100644 index 0000000..7903cde --- /dev/null +++ b/functions/New-SimulateTyping.ps1 @@ -0,0 +1,17 @@ +# gitea.RdzeN.net +# Simulate Typing +function New-SimulateTyping { +param( +[string]$message +) + +# Iterate through each character in the text +foreach ($char in $message.ToCharArray()) { + # Write each character to the screen + Write-Host -NoNewline $char -ForegroundColor Yellow + # Wait before writing the next character (adjustable time in milliseconds) + Start-Sleep -Milliseconds (Get-Random -Minimum 10 -Maximum 100) +} + # New line after last char. + Write-Host "" +} \ No newline at end of file diff --git a/functions/Open-Folder.ps1 b/functions/Open-Folder.ps1 new file mode 100644 index 0000000..063d19e --- /dev/null +++ b/functions/Open-Folder.ps1 @@ -0,0 +1,6 @@ +# gitea.RdzeN.net +# Open Folder in Explorer +function Open-Folder { + param ([string]$path = $PWD) + Start-Process Explorer -ArgumentList $path +} \ No newline at end of file diff --git a/functions/Override-Profile.ps1 b/functions/Override-Profile.ps1 new file mode 100644 index 0000000..feac173 --- /dev/null +++ b/functions/Override-Profile.ps1 @@ -0,0 +1,7 @@ +# gitea.RdzeN.net +# Override $profile +function Override-Profile { + if ((Test-Path $PROFILE)) { + Get-Content "$path_gitea_powershell_profile\RdzeN-Profile.ps1" | Set-Content $PROFILE -Force + } +} \ No newline at end of file diff --git a/functions/Reload-Profile.ps1 b/functions/Reload-Profile.ps1 new file mode 100644 index 0000000..0529748 --- /dev/null +++ b/functions/Reload-Profile.ps1 @@ -0,0 +1,5 @@ +# gitea.RdzeN.net +# Reload $profile +function Reload-Profile { + . $PROFILE +} \ No newline at end of file diff --git a/functions/Test-Profile.ps1 b/functions/Test-Profile.ps1 new file mode 100644 index 0000000..b3a0a87 --- /dev/null +++ b/functions/Test-Profile.ps1 @@ -0,0 +1,7 @@ +# gitea.RdzeN.net +# Reload $profile +function Test-Profile { + if (-not (Test-Path $PROFILE)) { + Copy-Item -Path "$path_gitea_powershell_profile\RdzeN-Profile.ps1" -Destination $PROFILE -force + } +} \ No newline at end of file diff --git a/functions/prompt.ps1 b/functions/prompt.ps1 new file mode 100644 index 0000000..c9f35ec --- /dev/null +++ b/functions/prompt.ps1 @@ -0,0 +1,35 @@ +# gitea.RdzeN.net +# +# custom prompt for PS. +function prompt +{ + Write-Host "[" -NoNewline -ForegroundColor Blue + Write-Host "PS" -NoNewline -ForegroundColor Cyan + Write-Host "]" -NoNewline -ForegroundColor Blue + Write-Host " " -NoNewline -ForegroundColor Black + Write-Host "[" -NoNewline -ForegroundColor Blue + Write-Host "$(get-date -Format "yyyy-MM-dd HH:mm:ss")" -NoNewline -ForegroundColor Cyan + Write-Host "]" -NoNewline -ForegroundColor Blue + Write-Host " " -NoNewline -ForegroundColor Black + Write-Host "[" -NoNewline -ForegroundColor Blue + Write-Host "$Env:USERNAME" -NoNewline -ForegroundColor Cyan + Write-Host "@" -NoNewline -ForegroundColor Blue + Write-Host "$env:COMPUTERNAME" -NoNewline -ForegroundColor Cyan + Write-Host "]" -NoNewline -ForegroundColor Blue + Write-Host " " -NoNewline -ForegroundColor Black + Write-Host "[" -NoNewline -ForegroundColor Blue + Write-Host "$($PWD.ProviderPath)" -NoNewline -ForegroundColor Cyan + Write-Host "]" -NoNewline -ForegroundColor Blue + Write-Host "" + + Write-Host "[" -NoNewline -ForegroundColor Blue + Write-Host "PS" -NoNewline -ForegroundColor Cyan + Write-Host "]" -NoNewline -ForegroundColor Blue + + Write-Host " " -NoNewline -ForegroundColor Black + Write-Host "$('>' * ($nestedPromptLevel + 1)) " -NoNewline -ForegroundColor Cyan + + # .Link + # https://go.microsoft.com/fwlink/?LinkID=225750 + # .ExternalHelp System.Management.Automation.dll-help.xml +} \ No newline at end of file