how to get directory full path per selected folder

Tagged: 

This topic contains 3 replies, has 4 voices, and was last updated by Profile photo of Graham Beer Graham Beer 3 weeks, 6 days ago.

  • Author
    Posts
  • #67309
    Profile photo of Pond
    Pond
    Participant

    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}
    
  • #67314
    Profile photo of MK
    MK
    Participant
    $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)"
    
  • #67318
    Profile photo of Sam Boutros
    Sam Boutros
    Participant
    $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..

  • #67321
    Profile photo of Graham Beer
    Graham Beer
    Participant

    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.

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

You must be logged in to reply to this topic.