Interactive Select from List in Powershell - powershell

Is there a plugin or tool that will let me display a list of objects to a user (Format-Table style) and allow them to use the cursor to select a choice from the list, including potentially scrolling a long list? I would like to be able to do something like this:
Get-User -anr $search |Get-Choice| Set-User -EnableAccount true
This script should display a list of matching accounts at a console prompt, allow the user to scroll up and down the list interactively, and select a choice by pressing Enter (or pass null if the user hits escape). Only one account would be passed to Set-User, rather than a list of all matching choices.
Obviously details could differ. While I would prefer a console version, a graphical one would be acceptable (that popped up a Windows dialog). The exact keystrokes could be different. But the core goal (accepting a list, getting user input, piping out the result) should be met.

in v3:
Get-User -anr $search | Out-GridView -PassThru | Set-User -EnableAccount true

Take a look to Out-Form
pseudo use:
out-form -title "Enable Account" -data (Get-user -anr $search) -columnNames ("AccountName") `
-columnProperties ("SamAccountName") -actions #{Enable It!" = { $_ | Set-User -EnableAccount true}}

Related

Passing previous Powershell command properties into a piped command

Can someone show how to pass a property from one Powershell command into the next piped command (in order to pull properties from both levels of the request)?
I need to get archive mailbox sizes for users in Exchange Online. An example of this is here:
Get-EXOMailbox | get-MailboxStatistics -archive | select displayname,totalitemsize
When you run this, obviously you are grabbing properties from the second command. I need to also grab the identity property tied to Get-EXOMAilbox, the first command (which shows the user's active mailbox and is useful for subsequent actions.
Thanks!
As Doug Maurer suggests, you might want to take advantage of the -PipelineVariable common parameter:
Get-EXOMailbox -PipelineVariable mailbox |Get-MailboxStatistics -Archive |Select #{Name='DisplayName';Expression={ $mailbox.DisplayName }},TotalItemSize

Powershell 5.0 pause function [duplicate]

Disclaimer : I am the epitome of a scipting/Powershell rookie, so please bear with me.
I've written a script to return the Active Directory username of any user currently logged into a given workstation.
$input = Read-Host "Workstation Name"
$domain = ".*****.***.com"
$computer = $input + $domain
$list = gwmi win32_computersystem -comp $computer | select Username,Caption
Write-Output $list
However, if I run this from a pinned script in the taskbar, the Powershell window closes before I have a chance to view the results.
I have tried method 2 and 3 from this post, but to no avail. Method 2 prompts for user input before the results are displayed instead of after, even when the code for the prompt is added at the end of the script.
Any help would be greatly appreciated.
Method 2 from the linked post - i.e., waiting for the user to press a key before exiting the script - can be used, but it requires additional effort:
End your script as follows in order to see the value of $list before the pause command prompts:
$list | Out-Host # Force *synchronous* to-display output.
pause # Wait for the user to press Enter before exiting.
Note: pause in PowerShell is simply a function wrapper around Read-Host as follows: $null = Read-Host 'Press Enter to continue...' Therefore, if you want to customize the prompt string, call Read-Host directly.
This answer explains why the use of Out-Host (or Format-Table) is necessary in this case; in short:
In PSv5+, an implicitly applied Format-Table command asynchronously waits for up to 300 msecs. for additional pipeline input, in an effort to derive suitable column widths from the input data.
Because you use Write-Output output objects without predefined formatting data that have 2 properties (4 or fewer ), tabular output is implicitly chosen, and Format-Table is used behind the scenes, asynchronously.
Note: The asynchronous behavior applies only to output objects for whose types formatting instructions aren't predefined (as would be reported with Get-FormatData <fullOutputTypeName>); for instance, the output format for the System.Management.Automation.AliasInfo instances output by Get-Alias is predefined, so Get-Alias; pause does produce output in the expected sequence.
The pause command executes before that waiting period has elapsed, and only after you've answered the prompt does the table print, after which point the window closes right away.
The use of an explicit formatting command (Out-Host in the most generic case, but any Format-* cmdlet will do too) avoids that problem by producing display output synchronously, so that the output will be visible by the time pause displays its prompt.
I had the same problem for scripts that I'm executing "on demand". I tend to simply add a Read-Host at the end of the script like so
$str = "This text is hardly readable because the console closes instantly"
Write-Output $str
Read-Host "Script paused - press [ENTER] to exit"

How do I prevent Powershell from closing after completion of a script?

Disclaimer : I am the epitome of a scipting/Powershell rookie, so please bear with me.
I've written a script to return the Active Directory username of any user currently logged into a given workstation.
$input = Read-Host "Workstation Name"
$domain = ".*****.***.com"
$computer = $input + $domain
$list = gwmi win32_computersystem -comp $computer | select Username,Caption
Write-Output $list
However, if I run this from a pinned script in the taskbar, the Powershell window closes before I have a chance to view the results.
I have tried method 2 and 3 from this post, but to no avail. Method 2 prompts for user input before the results are displayed instead of after, even when the code for the prompt is added at the end of the script.
Any help would be greatly appreciated.
Method 2 from the linked post - i.e., waiting for the user to press a key before exiting the script - can be used, but it requires additional effort:
End your script as follows in order to see the value of $list before the pause command prompts:
$list | Out-Host # Force *synchronous* to-display output.
pause # Wait for the user to press Enter before exiting.
Note: pause in PowerShell is simply a function wrapper around Read-Host as follows: $null = Read-Host 'Press Enter to continue...' Therefore, if you want to customize the prompt string, call Read-Host directly.
This answer explains why the use of Out-Host (or Format-Table) is necessary in this case; in short:
In PSv5+, an implicitly applied Format-Table command asynchronously waits for up to 300 msecs. for additional pipeline input, in an effort to derive suitable column widths from the input data.
Because you use Write-Output output objects without predefined formatting data that have 2 properties (4 or fewer ), tabular output is implicitly chosen, and Format-Table is used behind the scenes, asynchronously.
Note: The asynchronous behavior applies only to output objects for whose types formatting instructions aren't predefined (as would be reported with Get-FormatData <fullOutputTypeName>); for instance, the output format for the System.Management.Automation.AliasInfo instances output by Get-Alias is predefined, so Get-Alias; pause does produce output in the expected sequence.
The pause command executes before that waiting period has elapsed, and only after you've answered the prompt does the table print, after which point the window closes right away.
The use of an explicit formatting command (Out-Host in the most generic case, but any Format-* cmdlet will do too) avoids that problem by producing display output synchronously, so that the output will be visible by the time pause displays its prompt.
I had the same problem for scripts that I'm executing "on demand". I tend to simply add a Read-Host at the end of the script like so
$str = "This text is hardly readable because the console closes instantly"
Write-Output $str
Read-Host "Script paused - press [ENTER] to exit"

How to remove a field in a users id in active directory using PowerShell

What I am trying to do is that there are a lot of user id's in our Windows Active Directory that when you go to the properties of the id's, in the "Office" field, it says (for this example) "test this". I was wondering if there is a way to write in PowerShell to find all the users in Active Directory with "test this" written in the "office" field, and then to remove the words "test this" from the Office field.
Any help is greatly appreciated!
You can do this using the -LDAPFilter param for Get-AdUser, like so:
Get-ADUser -LDAPFilter “(office='test this')”
Should give you a listing of all the users with 'Test this' in their office property. Run that first and confirm it gives you the list you'd like.
Next, to mass change the value:
Get-ADUser -LDAPFilter “(office='test this')” | Set-AdUser -Clear office -whatif
Run that once in WhatIf mode to see what would happen, and if you're happy with it, then remove -Whatif to run it in production.

Out-GridView with Passthru for powershell 2?

Powershell v3 added the -Passthru switch to the Out-GridView cmdlet that allows user to select items that are returned back to the pipeline.
I'm looking for a similar solution for Powershell v2 (Dotnet 3.5/4.0). I need the option to pipe in some values, allow user to filter and choose multiple values, and return the selected items to the pipeline.
Is there a way run the new Out-GridView throught dotnet? or maybe someone created a code for something similar?
I've got this, FWIW:
Select -Item
Not a real grid view, but it works for V2, and in remoting sessions where GridView isn't an option.