Author Posts

March 27, 2017 at 7:42 am

Hi all

I am write POSH script to do the following:

1. get name of subfolders under D:\Users to variable.

2. list subfolders from variable and put 1,2,3 ... begin of the folder name and write to console. like.:
Please select user that you want to see directory full path.
1- user1
2- user2
3- user3

3. script will ask to user input number and print out directory full path per selected

However I can write code only get subfolder but don't know how to do further steps , can anyone guide me of this ??

here is my code.

$excludeList = 'admin', 'Administrator', 'All Users', 'Default', 'Default User', 'Public', 'TEMP'
$profileLists = Get-ChildItem D:\Users -Exclude $excludeList | 
       Where-Object {$_.PSIsContainer} | 
       Foreach-Object {$_.Name}

March 27, 2017 at 10:19 am

$excludeList = 'admin', 'Administrator', 'All Users', 'Default', 'Default User', 'Public', 'TEMP'
$profileLists = Get-ChildItem C:\Users -Exclude $excludeList | 
       Where-Object {$_.PSIsContainer}

Write-Host "Please select user that you want to see directory full path."
$number = 1
$profileLists | ForEach-Object {Write-Host "$number - $($_.Name)";$number++}
$input = Read-Host
Write-Host "Full path: $($profileLists[$input-1].FullName)"

March 27, 2017 at 10:23 am

$excludeList = 'admin', 'Administrator', 'All Users', 'Default', 'Default User', 'Public', 'TEMP'
$profileLists = Get-ChildItem c:\Users -Exclude $excludeList -Directory | select name, fullname | sort name
$i = 0; $profileLists | % { $i++; $_ | Add-Member NoteProperty ID $i }
$profileLists | % { "$($_.ID) - $($_.name)" }

$Scriptblock =  "$([char]36)Done = $([char]36)false; Do {"
$Scriptblock += 'switch (Read-Host ''Type in the number of the user to see full path'') {'
$Scriptblock += 0..$($profileLists.Count-1) | % {
    "$($_+1) { '$($profileLists[$_].fullname)'; $([char]36)Done = $([char]36)true }"
} | Out-String
$Scriptblock += "    default { ""Please select a number between 1 and $($profileLists.Count)"" } } } until ($([char]36)Done)"

Invoke-Expression $Scriptblock

Line 3 uses -Directory parameter of the Get-ChildItem cmdlet to pick directories only, selects both name and fullname properties
Line 4 adds a sequential number to each item in the $profileLists array
The $Scriptblock is an example of code that writes code. The purpose of the $Scriptblock is to generate a dynamic switch statement with as many options as we have elements in the $profileLists array..

March 27, 2017 at 10:24 am

Hi Pond,

Here are a couple of good articles about a Powershell menu.

One is a more complicated and makes use of .net and the other is a bit more straight forward. Should help with what you're after.

Building a PowerShell Console Menu Revisited, Part 1


https://technet.microsoft.com/en-us/library/ff730939.aspx