locked
Get-Content as Variable changes BaseType depending on Variable Name - Why? RRS feed

  • Question

  • I have a simple text file with usernames:

    don.hall
    kim.akers
    sales1
    sales10

    Get-Content -Path 'C:\temp\users.txt' displays correctly:

    don.hall
    kim.akers
    sales1
    sales10

    $a = Get-Content -Path 'C:\temp\users.txt' displays correctly:

    don.hall
    kim.akers
    sales1
    sales10

    and is and array:

    PS C:\Windows\system32> $a.GetType()

    IsPublic IsSerial Name                                     BaseType                                                                                       
    -------- -------- ----                                     --------                                                                                       
    True     True     Object[]                                 System.Array 

    however, changing the variable name makes everything wrong:

    $users = Get-Content -Path 'C:\temp\users.txt' displays incorrectly and loses carriage returns

    don.hall kim.akers sales1 sales10

    and is no longer an Array:

    PS C:\Windows\system32> $users.GetType()

    IsPublic IsSerial Name                                     BaseType                                                                                       
    -------- -------- ----                                     --------                                                                                       
    True     True     String                                   System.Object

    why is this??


    MCSA Server 2012/2008 MCITP Exchange 2010

    Wednesday, July 5, 2017 1:38 PM

Answers

  • very odd, but I just put the exact same code into PowerShell again today (file hasn't changed or been touched)

    its showing correctly as an array:

    PS C:\Windows\system32> $users = Get-Content -Path 'C:\Temp\users.txt'
    
    PS C:\Windows\system32> $users.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                             
    -------- -------- ----                                     --------                                                                                                                                             
    True     True     Object[]                                 System.Array 

    so I'm a bit stumped on that one.

    I'm now wondering whether I initially created $users earlier in the session with different data which was originally a string and it kept the String format even though I changed what it was pointing at and was expecting an array? Does a variable remain 'static' in terms of BaseType once created?



    MCSA Server 2012/2008 MCITP Exchange 2010

    • Marked as answer by Guyver-1 Thursday, July 20, 2017 11:13 AM
    Thursday, July 20, 2017 11:13 AM

All replies

  • The file is not a correct Windows file.`  It does not have CrLf pairs but likely has on an LF at the end of each line.


    \_(ツ)_/

    Wednesday, July 5, 2017 2:09 PM
  • I don't think you read the topic correctly

    MCSA Server 2012/2008 MCITP Exchange 2010

    Thursday, July 6, 2017 8:27 AM
  • I don't think you read the topic correctly

    MCSA Server 2012/2008 MCITP Exchange 2010

    It is the only explanation and it is clear your demo is either missing information or is just a mistake.


    \_(ツ)_/

    Thursday, July 6, 2017 8:30 AM
  • So explain why $a can read it correctly and output it as an array but $users doesn't?

    its the same file each time, only the variable name is changing, not the file the variable is reading.

    Your logic dictates that its the 'file' that is the problem when I'm clearly demonstrating that its the variable name that's the issue as the same file is being read each time.

    file created on the server using notepad.


    MCSA Server 2012/2008 MCITP Exchange 2010

    Thursday, July 20, 2017 10:47 AM
  • We cannot reproduce your problem.


    \_(ツ)_/

    Thursday, July 20, 2017 11:10 AM
  • very odd, but I just put the exact same code into PowerShell again today (file hasn't changed or been touched)

    its showing correctly as an array:

    PS C:\Windows\system32> $users = Get-Content -Path 'C:\Temp\users.txt'
    
    PS C:\Windows\system32> $users.GetType()
    
    IsPublic IsSerial Name                                     BaseType                                                                                                                                             
    -------- -------- ----                                     --------                                                                                                                                             
    True     True     Object[]                                 System.Array 

    so I'm a bit stumped on that one.

    I'm now wondering whether I initially created $users earlier in the session with different data which was originally a string and it kept the String format even though I changed what it was pointing at and was expecting an array? Does a variable remain 'static' in terms of BaseType once created?



    MCSA Server 2012/2008 MCITP Exchange 2010

    • Marked as answer by Guyver-1 Thursday, July 20, 2017 11:13 AM
    Thursday, July 20, 2017 11:13 AM
  • There is no accounting how human foibles can disrupt something as simple as a computer.  Suspect that your computer is infested with demons.  Get an exorcist to help you.


    \_(ツ)_/

    Thursday, July 20, 2017 11:16 AM
  • hahaha :)



    MCSA Server 2012/2008 MCITP Exchange 2010

    Thursday, July 20, 2017 11:19 AM