locked
[IO.Directory]::GetCurrentDirectory() gives different results on different PCs RRS feed

  • Question

  • On one Windows 7 Professional PC, I have:

    07:35 D:\news\2011> get-location
    
    Path
    ----
    D:\news\2011
    
    
    07:35 D:\news\2011> [io.directory]::getcurrentdirectory()
    C:\Users\Administrator
    07:35 D:\news\2011>

    On another Windows 7 Professional PC, I have:

    07:34 D:\News\2011> get-location
    
    Path
    ----
    D:\News\2011
    
    
    07:36 D:\News\2011> [io.directory]::getcurrentdirectory()
    D:\news\2011
    07:36 D:\News\2011>

    Why the inconsistency?

     

    Sunday, October 9, 2011 11:43 PM

Answers

  • http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx

    Retrieves the current directory for the current process.

    So... Get-location get's the directory you are currently browsing... 

    [io.directory]::getcurrentdirectory() will show you the directory in which you were when you started the process

     


    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog
    • Proposed as answer by Oliver Lipkau Sunday, October 9, 2011 11:52 PM
    • Marked as answer by K.Kong Monday, October 10, 2011 12:35 AM
    • Edited by Oliver Lipkau Monday, October 10, 2011 2:51 AM reformulating
    Sunday, October 9, 2011 11:51 PM

All replies

  • http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx

    Retrieves the current directory for the current process.

    So... Get-location get's the directory you are currently browsing... 

    [io.directory]::getcurrentdirectory() will show you the directory in which you were when you started the process

     


    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog
    • Proposed as answer by Oliver Lipkau Sunday, October 9, 2011 11:52 PM
    • Marked as answer by K.Kong Monday, October 10, 2011 12:35 AM
    • Edited by Oliver Lipkau Monday, October 10, 2011 2:51 AM reformulating
    Sunday, October 9, 2011 11:51 PM
  • Do the following:

    1. Open Start/Run
    2. type: cmd
    3. type cd

    What do you see?

    In PowerShell type: [io.directory]::getcurrentdirectory()

    They will be the same.

    Why?

    Now in powershell type $home

    Do you see what is happenning?

    The framework gets the session current folder.  PowerShell gets its own current folder.

    Type:

    cd c:\windows
    pwd
    $home
    [io.directory]::getcurrentdirectory()

    Everything has its place.  The system has a home.  PowerSHell has a home and PowerShell has a current folder.

     

     

     


    jv
    • Edited by jrv Monday, October 10, 2011 12:02 AM
    Sunday, October 9, 2011 11:58 PM
  • http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx

    Retrieves the current directory for the current process.

    So... Get-location get's the directory you are currently browsing... 

     


    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog

    [io.directory]::getcurrentdirectory() will show you the location for the process you started


    Sorry Oliver but that is not what the MS posting is saying.  It is showing the folder that the process was launched in.

    PowerShell maintains its own current folder. PWD is what it is.  $home is where we start.

    [io.directory]::getcurrentdirectory()

    will always show the folder that PowerShell was launched from.

    type at a CMD prompt.

    cd somelocation
    powershell
    [io.directory]::getcurrentdirectory()
    $home
    pwd
    cd c:\windows
    pwd
    [io.directory]::getcurrentdirectory()

    Powershell wil change but the IO version wil lnever change unless ytou use the framework to alter teh process path.

    In a way you are correct but your post does not explain what PowerShell is doing. 

     

     
    jv
    Monday, October 10, 2011 1:31 AM
  • pwd is Get-location... which shows you the path you are currently in (in you powershell session)... same as "cd" in ms-dos.

    What's the problem? :-S

    Didn't understand what was unclear about my post


    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog
    Monday, October 10, 2011 1:48 AM
  • [io.directory]::getcurrentdirectory()

    Does not show the location of the process you started it shows the location that was 'current' when you started the process.

    I have seen may people including seasoned programmers make the same mistake.

    There is a location for the process and a 'current' location when the process is started and a setting for the current folder for the newly created process.

    Look at the API for 'CreateProcess'. It will show you how a process 'can' get started.  The location is either defaulted or explicitly set.

    Are you still in doubt?

     

     


    jv
    Monday, October 10, 2011 2:05 AM
  • nope... I've got it... we were having some language problems, because both meant the same thing :-)
    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog
    Monday, October 10, 2011 2:08 AM
  • nope... I've got it... we were having some language problems, because both meant the same thing :-)
    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog


    Ok - but logically I don't believe your statement is the same.  That was what I was pointing at.

    If you were trying to say the same thing then OK.  A subtle misunderastanding here is what causes a lot of people to become frustrated and to declare that PowerShell, VBScript and Windows have serious bugs.  It is what it is and it has always been defined that way. PowerShell is no different.

     


    jv
    Monday, October 10, 2011 2:46 AM
  • there... I edited my post clarify what I meant.
    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog
    Monday, October 10, 2011 2:52 AM
  • there... I edited my post clarify what I meant.
    Oliver Lipkau
    [PowerShell enthusiast]
    http://oliver.lipkau.net/blog


    Sorry - I am not picking o you.  It is not a langauge problem but a logic problem.

    Language that is used everyday can be very problematic when trying ot make a logical statement.

    Wittgenstein fought through this issue for many years.  He thought he could logically formalize language. What he ended up giving us was a good lexicon of dos and don'ts.

    I believe you meant the same thing but your statement seems to say otherwise.  If I was wrong I appologize.

     

     


    jv
    Monday, October 10, 2011 3:16 AM