functionInvoke-Selection { try { # get the selected text: $selectedText = $psise.CurrentFile.Editor.SelectedText # if no text was selected... if ($selectedText.Length -eq0) { # ...select the line the caret is in and get this line instead: $psise.CurrentFile.Editor.SelectCaretLine() $selectedText = $psise.CurrentFile.Editor.SelectedText }
# try and parse the code $sb = [ScriptBlock]::Create($selectedText)
# get the missing variable content from the underlying file: $currentFile = $psise.CurrentFile.FullPath $currentFolder = Split-Path-Path$currentFile
# append the selected code with these automatic variables, and set them: $runcode = @" `$PSCommandPath = '$currentFile' `$PSScriptRoot = '$currentFolder' $selectedText "@ # turn text into script block... $scriptblock = [ScriptBlock]::Create($runcode) # ...and execute it without private scope: . $scriptblock
# script may require Administrator privileges if you want to remove # module versions installed in "AllUsers" scope
# find ALL modules with more than one version and/or location: $multiversion = Get-Module-ListAvailable | Group-Object-Property Name | Sort-Object-Property Name | Where-Object Count -gt1
# ask user WHICH of these modules to clean? $clean = $multiversion | Select-Object-Property@{N='Versions';E={$_.Count}}, @{N='ModuleName';E={$_.Name}} | Out-GridView-Title'Select module(s) to clean'-PassThru
# get the todo list with the modules the user wants to clean: $todo = $multiversion | Where-Object Name -in$clean.ModuleName
$todo | ForEach-Object { $module = $_.Name # list all versions of a given module and let the user decide which versions # to keep and which to remove: $_.Group | Select-Object-Property Version, ModuleBase, ReleaseNotes | Sort-Object-Property Version | Out-GridView-Title"Module $module : Select all versions that you want to remove"-PassThru | Select-Object-ExpandProperty ModuleBase | # do a last confirmation dialog before permanently deleting the subversions: Out-GridView-Title'Do you really want to permanently delete these folders? CTRL+A and OK to confirm'-PassThru | Remove-Item-Recurse-Force
# folders where PowerShell looks for modules: $paths = $env:PSModulePath-split';' # finding actual module folders $modules = Get-ChildItem-Path$paths-Depth0-Directory | Sort-Object-Property Name
$modules | Select-Object-Property Name, @{N='Parent';E={$_.Parent.FullName}}, FullName | Out-GridView-Title'Select module(s) to permanently delete'-PassThru | Out-GridView-Title'Do you REALLY want to remove the modules below? CTRL+A and OK to confirm'-PassThru | Remove-Item-Path { $_.FullName } -Recurse-Force-WhatIf# remove -WhatIf to actually delete (as always at own risk)