Last active
June 12, 2026 15:05
-
-
Save disouzam/ae08fc48552c22cdb6532973ebd2ae70 to your computer and use it in GitHub Desktop.
PowerShell profile and functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # https://lazyadmin.nl/powershell/powershell-profile/ | |
| # Set Default location | |
| Set-Location C:\ | |
| function Install-Python-Dependencies { | |
| # https://www.w3tutorials.net/blog/utf8-script-in-powershell-outputs-incorrect-characters/ | |
| # Set output encoding to UTF-8 | |
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
| # Set input encoding to UTF-8 (for reading user input with non-ASCII chars) | |
| [Console]::InputEncoding = [System.Text.Encoding]::UTF8 | |
| poetry install --no-root | |
| } | |
| # Alias for common commands | |
| New-Alias -Name 'pid' -Value 'Install-Python-Dependencies' | |
| function CustomizeConsole { | |
| $consoleCreationTime = Get-Date -Format "dd/MM/%y HH:mm" | |
| $hostversion="$($Host.Version.Major).$($Host.Version.Minor)" | |
| $Host.UI.RawUI.WindowTitle = "PSCore $hostversion ($consoleCreationTime)" | |
| Clear-Host | |
| } | |
| CustomizeConsole | |
| function GetSHA256OfAllFiles { | |
| # https://www.w3tutorials.net/blog/utf8-script-in-powershell-outputs-incorrect-characters/ | |
| # Set output encoding to UTF-8 | |
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
| # Set input encoding to UTF-8 (for reading user input with non-ASCII chars) | |
| [Console]::InputEncoding = [System.Text.Encoding]::UTF8 | |
| # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.6#inputs | |
| $now = Get-Date -AsUTC | |
| $currentDateTime = Get-Date $now -UFormat "%FT%H-%M-%S" | |
| $currentDateTime = "{0}-{1:D3}Z" -f $currentDateTime, $now.Millisecond | |
| $fileFullName = (Resolve-Path -Path "..\").Path + "\$($currentDateTime)-Files-SHA256-Size.csv" | |
| Get-ChildItem -Path . -Recurse -File -Force | ForEach-Object { | |
| if ($_.FullName -ne $fileFullName) { | |
| $hash = Get-FileHash -LiteralPath "$($_.FullName)" -Algorithm SHA256 | Select-Object -ExpandProperty Hash | |
| } else { | |
| continue | |
| } | |
| $fileSize = $_.Length | |
| [PSCustomObject]@{ | |
| Path = $_.FullName | |
| SHA256 = $hash | |
| SizeMB = [math]::Round($fileSize / 1MB, 6) | |
| Modified = $_.LastWriteTime | |
| } | |
| } | Export-Csv -Path $fileFullName -NoTypeInformation -Encoding UTF8 | |
| Write-Host "File information saved at $fileFullName" | |
| } | |
| function CountFiles { | |
| $fileCount = (Get-ChildItem -Path . -File).Count | |
| Write-Output "Number of files: $fileCount" | |
| } | |
| function Move-GenericFilesWithDuplicates { | |
| [CmdletBinding()] | |
| param ( | |
| [Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] | |
| [Alias('FullName','PSPath','Path')] | |
| [string[]]$FilePaths, | |
| [Parameter(Mandatory)] | |
| [string]$TargetFolder | |
| ) | |
| process { | |
| foreach ($filePath in $FilePaths) { | |
| if (-not (Test-Path $filePath)) { | |
| Write-Warning "File not found: $filePath" | |
| continue | |
| } | |
| $fileName = [System.IO.Path]::GetFileName($filePath) | |
| $destination = Join-Path -Path $TargetFolder -ChildPath $fileName | |
| $baseName = [System.IO.Path]::GetFileNameWithoutExtension($fileName) | |
| $extension = [System.IO.Path]::GetExtension($fileName) | |
| $counter = 2 | |
| while (Test-Path $destination) { | |
| $destination = Join-Path -Path $TargetFolder -ChildPath ("$baseName ($counter)$extension") | |
| $counter++ | |
| } | |
| Move-Item -Path $filePath -Destination $destination -Verbose | |
| } | |
| } | |
| } | |
| # Function to get OneDrive sync status | |
| function Get-OneDriveStatus { | |
| param([string]$Path) | |
| # https://www.w3tutorials.net/blog/utf8-script-in-powershell-outputs-incorrect-characters/ | |
| # Set output encoding to UTF-8 | |
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
| # Set input encoding to UTF-8 (for reading user input with non-ASCII chars) | |
| [Console]::InputEncoding = [System.Text.Encoding]::UTF8 | |
| if ([string]::IsNullOrWhiteSpace($Path)) { | |
| throw "Path cannot be null or empty." | |
| } | |
| # Handle relative paths by converting them to absolute paths | |
| if (-not [System.IO.Path]::IsPathRooted($Path)) { | |
| $Path = Join-Path -Path (Get-Location).Path -ChildPath $Path | |
| } | |
| $resolvedPath = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path | |
| try { | |
| $shell = New-Object -ComObject Shell.Application | |
| $folder = $shell.Namespace((Split-Path $resolvedPath -Parent)) | |
| $file = $folder.ParseName((Split-Path $resolvedPath -Leaf)) | |
| if (-not $folder -or -not $file) { | |
| throw "Unable to parse path: $resolvedPath" | |
| } | |
| # Column indexes for OneDrive status (may vary by Windows version) | |
| $status = $folder.GetDetailsOf($file, 296) # Availability status | |
| $sync = $folder.GetDetailsOf($file, 303) # Sync status | |
| $sha256 = "Not Available" | |
| try{ | |
| if ($sync -in @('Available on this device', 'Always available on this device')) { | |
| $sha256 = (Get-FileHash -LiteralPath $resolvedPath -Algorithm SHA256).Hash | |
| } | |
| } | |
| catch { | |
| $sha256 = "Error: " + $_.Exception.Message | |
| }ex | |
| return [PSCustomObject]@{ | |
| Path = $resolvedPath | |
| Availability = $status | |
| SyncStatus = $sync | |
| SHA256 = $sha256 | |
| } | |
| } | |
| catch { | |
| return [PSCustomObject]@{ | |
| Path = if ($resolvedPath) { $resolvedPath } else { $Path } | |
| Availability = "Error" | |
| SyncStatus = "Error" | |
| ErrorCode = if ($_.Exception.HResult) { ('0x{0:X8}' -f $_.Exception.HResult) } else { "Unknown" } | |
| ErrorMessage = $_.Exception.Message | |
| } | |
| } | |
| } | |
| function Export-OneDriveStatusAllFiles { | |
| [CmdletBinding()] | |
| param( | |
| [Parameter()] | |
| [string]$Path = "." | |
| ) | |
| # https://www.w3tutorials.net/blog/utf8-script-in-powershell-outputs-incorrect-characters/ | |
| [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 | |
| [Console]::InputEncoding = [System.Text.Encoding]::UTF8 | |
| # https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.6#inputs | |
| $now = Get-Date -AsUTC | |
| $currentDateTime = Get-Date $now -UFormat "%FT%H-%M-%S" | |
| $currentDateTime = "{0}-{1:D3}Z" -f $currentDateTime, $now.Millisecond | |
| $fileFullName = (Resolve-Path -Path "..\").Path + "\$($currentDateTime)-OneDrive-Status.csv" | |
| Write-Host "Analysis started and file will be saved at: $fileFullName" | |
| if (-not [System.IO.Path]::IsPathRooted($Path)) { | |
| $Path = Join-Path -Path (Get-Location).Path -ChildPath $Path | |
| } | |
| $resolvedRoot = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path | |
| $files = Get-ChildItem -LiteralPath $resolvedRoot -Recurse -File -Force | |
| $total = $files.Count | |
| $counter = 0 | |
| $files | ForEach-Object { | |
| $counter++ | |
| Write-Host -NoNewline "`rProcessing files: $counter/$total" | |
| Get-OneDriveStatus -Path $_.FullName | |
| } | Export-Csv -Path $fileFullName -NoTypeInformation -Encoding UTF8 | |
| Write-Host "" | |
| Write-Host "OneDrive status saved at $fileFullName" | |
| } | |
| function Kill-ProcessByPort { | |
| param( | |
| [Parameter(Mandatory)] | |
| [int]$Port | |
| ) | |
| $processes = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue | Select-Object -ExpandProperty OwningProcess -Unique | |
| if ($processes) { | |
| foreach ($processId in $processes) { | |
| try { | |
| Stop-Process -Id $processId -Force -ErrorAction Stop | |
| Write-Host "Killed process with PID: $processId using port: $Port" | |
| } | |
| catch { | |
| Write-Warning "Failed to kill process with PID: $processId. Error: $_" | |
| } | |
| } | |
| } | |
| else { | |
| Write-Host "No processes found using port: $Port" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment