ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell Get-Childitem behavior with variables

    IT Discussion
    windows server 2012 r2 powershell scripting
    5
    11
    2.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • EddieJenningsE
      EddieJennings
      last edited by

      Here's something I ran into yesterday. Perhaps those more knowledgeable of Windows than I can shed some light onto what's going on, or tell me if my thinking is wrong.

      Take this command,

      Get-ChildItem -Path C:\ -Include *.txt -Recurse -ErrorAction SilentlyContinue

      As expected, this will return any file that exists with .txt at the end of the filename anywhere within the C:\ directory tree. If you happen to try to list items from something you can't access, error messages are suppressed.

      Let's say I want to store data in a variable, so I can more easily manipulate it. I'd run this command.

      $foo = Get-ChildItem -Path C:\ -Include *.txt -Recurse -ErrorAction SilentlyContinue

      In my environment of Server 2012 R2, domain-joined machine, running as the local, built-in Administrator account, the first command works flawlessly, while the second command throws an "Access is Denied" error.

      Tinkering and experimenting led me to discover the likely problem is that even the Administrator account cannot read this hidden directory C:\Documents and Settings. It looks like this is some kind of a link to actual user profiles. I can cd into the directory, but not list its contents. I can further run this cd c:\Documents and Settings\Administrator and see the same contents as C:\Users\Administrator.

      The fact that I cannot list the contents of C:\Documents and Settings really isn't a problem, since the error should be suppressed by -ErrorAction. I don't understand why there is a difference in behavior when I try to assign this command to a variable.

      Anyone else experience this?

      1 Reply Last reply Reply Quote 2
      • EddieJenningsE
        EddieJennings
        last edited by

        Two more tidbits.

        This happens even when running as the system account. If I run an elevated command prompt and run psexec.exe -i -s -accepteula powershell.exe to get a PowerShell session as System, the behavior persists.

        I have found what appears to be a workaround: Use -OutVariable foo at the end of the command. This still lists all of the results of Get-ChildItem for me, but at least it did also store it in a variable.

        1 Reply Last reply Reply Quote 0
        • EddieJenningsE
          EddieJennings
          last edited by

          More information from experimenting.

          Using Out-variable doesn't actually make much of a different. Error is still occurring (didn't notice it the first time).

          Also, apparently the command with no variable is having access problems. Methinks now there's something screwy with this particular server.

          dafyreD 1 Reply Last reply Reply Quote 0
          • jmooreJ
            jmoore
            last edited by

            That could be Eddie. I'm experimenting and checking behavior with some of these commands.

            1 Reply Last reply Reply Quote 0
            • dafyreD
              dafyre @EddieJennings
              last edited by

              @eddiejennings said in Powershell Get-Childitem behavior with variables:

              More information from experimenting.

              Using Out-variable doesn't actually make much of a different. Error is still occurring (didn't notice it the first time).

              Also, apparently the command with no variable is having access problems. Methinks now there's something screwy with this particular server.

              Do you still get the access issues if you're running powershell as admin?

              EddieJenningsE 1 Reply Last reply Reply Quote 0
              • EddieJenningsE
                EddieJennings @dafyre
                last edited by

                @dafyre said in Powershell Get-Childitem behavior with variables:

                @eddiejennings said in Powershell Get-Childitem behavior with variables:

                More information from experimenting.

                Using Out-variable doesn't actually make much of a different. Error is still occurring (didn't notice it the first time).

                Also, apparently the command with no variable is having access problems. Methinks now there's something screwy with this particular server.

                Do you still get the access issues if you're running powershell as admin?

                Yes. Logged in as the local administrator account, as well as running as admin (which is the only option for this account).

                jmooreJ 1 Reply Last reply Reply Quote 0
                • jmooreJ
                  jmoore @EddieJennings
                  last edited by

                  @eddiejennings I assume you also right clicked and selected to [run as administrator]?
                  My account is an admin also but if i don't do the above some things don't work.

                  EddieJenningsE 1 Reply Last reply Reply Quote 1
                  • momurdaM
                    momurda
                    last edited by

                    This is likely because "Documents and Settings" isnt a real folder, just a junction point since Windows 7/Server 2008. Same reason you usually cant open this folder in Explorer.
                    These shortcuts only exist for legacy Windows things to function. You wont find this fake folder in Windows 10.

                    1 Reply Last reply Reply Quote 1
                    • black3dynamiteB
                      black3dynamite
                      last edited by black3dynamite

                      It probably has something to do with it being a junction point.

                      cmd /c "dir /al" will show that its a junction point to C:\Users

                      1 Reply Last reply Reply Quote 1
                      • EddieJenningsE
                        EddieJennings @jmoore
                        last edited by

                        @jmoore said in Powershell Get-Childitem behavior with variables:

                        @eddiejennings I assume you also right clicked and selected to [run as administrator]?
                        My account is an admin also but if i don't do the above some things don't work.

                        I did.

                        1 Reply Last reply Reply Quote 0
                        • EddieJenningsE
                          EddieJennings
                          last edited by

                          Must've been that one server. Seems like it's running fine on another one.

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post