New-ArrayFromClipboard.psm1
function New-ArrayFromClipboard{ <# .SYNOPSIS Create a PowerShell Array from Clipboard Data .DESCRIPTION Create a PowerShell Array from Clipboard Data, can set to a Variable, convert to Text for use in a script and copy back to Clipboard .NOTES Authors : Simon Godefroy - Simon.Godefroy@FocusedIT.co.uk Version : 1.0.3 Date : 2022.04.25 Update : 1.0.3 SG - 2022.04.25 Fixed Author details Update : 1.0.2 SG - 2022.04.24 Updated Company Name Update : 1.0.1 SG - 2022.04.05 Added Tab as Delimiter option Re-ordered Variable creation Update : 1.0.0 SG - 2022.04.04 Initial Script Create a PowerShell Array from Clipboard Data, can set to a Variable, convert to Text for use in a script and copy back to Clipboard .LINK http://www.FocusedIT.co.uk #> [cmdletbinding()] param ( [Parameter()] [ValidateSet("Autodetect","Comma","NewLine","SemiColon","Tab")] [string]$Delimiter = "Autodetect", [string]$VariableName, [switch]$AsText, [switch]$BackToClipboard #, ) try{ if(Get-Clipboard){ $ClipData = Get-Clipboard }else{ throw "Clipboard Empty or not text" } }catch{ Write-Error $global:Error[0] -ErrorAction Stop } $ClipData = $ClipData |?{$_ -notlike $null} $NewArray = @() if($Delimiter -like "Autodetect"){ Write-Verbose "Attempting to Autodetect Delimiter" #Test NewLine $NLItems = $ClipData.Split("`n") $NewLineCount = $NLItems.Count if($NewLineCount -gt 1){ Write-Verbose "NewLine detected" $ArrayData = $NLItems } if(!($ArrayData)){ #Test Comma $CItems = $ClipData.Split(",") $CommaCount = $CItems.Count if($CommaCount -gt 1){ Write-Verbose "Comma detected" $ArrayData = $CItems } } if(!($ArrayData)){ #Test SemiColon $SCItems = $ClipData.Split(";") $SemiColonCount = $SCItems.Count if($SemiColonCount -gt 1){ Write-Verbose "SemiColon detected" $ArrayData = $SCItems } } if(!($ArrayData)){ #Test Tab $TabItems = $ClipData.Split("`t") $TabColonCount = $TabItems.Count if($TabColonCount -gt 1){ Write-Verbose "Tab detected" $ArrayData = $TabItems } } }else{ if($Delimiter -like "Comma"){ $Split = "," }elseif($Delimiter -like "SemiColon"){ $Split = ";" }elseif($Delimiter -like "NewLine"){ $Split = "`n" }elseif($Delimiter -like "Tab"){ $Split = "`t" } try{ if(!($Split)){ throw "No Delimiter detected" } }catch{ Write-Error $global:Error[0] -ErrorAction Stop } $DetectedItems = $ClipData.Split("$Split") $DetectedCount = $DetectedItems.Count if($DetectedCount -gt 1){ $ArrayData = $DetectedItems } } try{ if(!($ArrayData)){ throw "No data detected (Number of items must be more than 1)" } }catch{ Write-Error $global:Error[0] -ErrorAction Stop } foreach($ArrayItem in $ArrayData){ $NewArray += $ArrayItem } if($AsText){ if($VariableName){ $TextString = "`$$VariableName = " }else{ $TextString = "" } $TextString+="@(" foreach($TextItem in $NewArray){ if($TextString -like "*("){ $TextString += "`"$TextItem`"" }else{ $TextString += ",`"$TextItem`"" } } $TextString +=")" $Output = $TextString }else{ $Output = $NewArray } if($BackToClipboard){ Write-Verbose "Writing Output back to Clipboard" $Output | Clip } if($VariableName){ if (Test-Path variable:$VariableName) { if(Get-Variable $VariableName -ErrorAction SilentlyContinue){ Write-Verbose "Previous Variable detected - Removing" Remove-Variable -Name $VariableName -Scope Global -Force } } New-Variable -Name $VariableName -Value $NewArray -Scope Global Write-Host "Global Variable: " -NoNewLine Write-Host "$VariableName" -ForegroundColor Cyan } $Output } |