store items in variable - Powershell

Kelly Garcia
Kelly Garcia used Ask the Experts™
on
Hi All,

I have this line of code in my script:

if ($null -ne $ListView.SelectedItems) {
        $ListView.ItemsSource = @($ListView.SelectedItems | Select-Object DisplayName, TotalItemSize, ItemCount, Database, TotalDeletedItemSize,DeletedItemCount, LastLoggedOnUserAccount, LastLogonTime, LastLogoffTime -OutVariable $mailboxtomigrate)
		$m = $mailboxtomigrate | Out-String
		[System.Windows.MessageBox]::Show("$m")

Open in new window


the selected can be many. I need to store it in a variable so I can loop through it when I run the new-moverequest script.

so I need the display names to be stored as a string in the variable e.g. $mailboxtomigrate = "display name 1", "display name 2", "display name 3"

how do I do this?

is better if I out the results into an array?  how do I do this?

thank you in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Raheman M. AbdulMessaging and Directory Services

Commented:
if ($null -ne $ListView.SelectedItems) {
        $ListView.ItemsSource = @($ListView.SelectedItems | Select-Object DisplayName, TotalItemSize, ItemCount, Database, TotalDeletedItemSize,DeletedItemCount, LastLoggedOnUserAccount, LastLogonTime, LastLogoffTime )
		$m = $ListView.ItemsSource  | Out-String
		[System.Windows.MessageBox]::Show("$m")

Open in new window

Chris DentPowerShell Developer
Top Expert 2010

Commented:
I think this will never actually be null:
if ($null -ne $ListView.SelectedItems) {

Open in new window

On reflection, it might be better to make the comparison:
if ($ListView.SelectedItems.Count -eq 0) {

Open in new window

How are you launching the code to migrate? I will continue to argue that you do not need to store these in another variable, they already are by virtue of having been selected. The GUI persists, and it's selections, persist until you change them or close it.
Senior Systems Administrator
Commented:
This works well for me:

if ($null -ne $ListView.SelectedItems) {
        $ListView.ItemsSource = @($ListView.SelectedItems | Select-Object DisplayName, TotalItemSize, ItemCount, Database, TotalDeletedItemSize,DeletedItemCount, LastLoggedOnUserAccount, LastLogonTime, LastLogoffTime -OutVariable mailboxtomigrate)
		$global:m = $mailboxtomigrate.displayname
		[System.Windows.MessageBox]::Show('You have Selected Mailboxes: ' + $m)
		
    	}

Open in new window

Kelly GarciaSenior Systems Administrator

Author

Commented:
best solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial