locked
Get a random subfolder from a folder and check it's permissions RRS feed

  • Question

  • Hello,

    I'm trying to come up with a way to pick a random subfolder from a fixed folder and view it's access permissions.

    This is what I came up with:

     
    $RandomFolder = Get-Random -InputObject (Get-ChildItem D:\Test)
    ((Get-Item $RandomFolder).GetAccessControl('Access')).Access

    But for some reason the Get-Item function searches the C drive instead of the D drive, tohugh it properly identifies a subfolder (SubTest2 in this example):

    Get-Item : Cannot find path 'C:\Windows\system32\SubTest2' because it does not exist.

    What am I doing wrong?

    Kind regards,

    Wojciech

    Tuesday, June 20, 2017 10:51 AM

Answers

  • Alternatively, to get a random subdirectory in a path even if not numbered:


    Get-ChildItem "D:\Test\SubTest*" -Directory | Get-Random


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by rozanw Tuesday, June 20, 2017 2:32 PM
    Tuesday, June 20, 2017 2:23 PM

All replies

  • What does "pick a random folder" mean? What problem are you trying to solve?

    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 20, 2017 12:26 PM
  • Folder structure:

    Test

        |-> SubTest1

        |-> SubTest2

        |-> SubTest3

        .

        .

        |-> SubTestN

    I need to pick one of the SubTest folders, chosen randomly, and check it's security permissions.

    Kind regards,

    Wojciech

    Tuesday, June 20, 2017 12:37 PM
  • What problem does this solve? Just use Explorer and right-click one of the folders and look at the permissions.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 20, 2017 12:40 PM
  • I am trying to automate this so that I don't have to use the Explorer. This is just one task in a particular activity. I was able to do everything else uing PowerShell and I want to do the same here.

    Kind regards,

    Wojciech

    Tuesday, June 20, 2017 1:04 PM
  • Homework question?

    \_(ツ)_/

    Tuesday, June 20, 2017 1:34 PM
  • Here's one way to get a random-numbered folder in a range:


    Get-Item ("D:\Test\SubTest{0}" -f (Get-Random -Minimum 1 -Maximum 200))
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 20, 2017 2:05 PM
  • Alternatively, to get a random subdirectory in a path even if not numbered:


    Get-ChildItem "D:\Test\SubTest*" -Directory | Get-Random


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by rozanw Tuesday, June 20, 2017 2:32 PM
    Tuesday, June 20, 2017 2:23 PM
  • This one worked, though I slightly modified it:

    $RandomFolder = Get-ChildItem "D:\Test\*" -Directory | Get-Random
    ((Get-Item "$RandomFolder").GetAccessControl('Access')).Access

    Thanks for your help.

    Wojciech

    Tuesday, June 20, 2017 2:33 PM