locked
Need little help in variable RRS feed

  • Question

  • I am new in Powershell. so i am messing with variable ...

    PS C:\WINDOWS\system32> Get-Content 'C:\Users\Administrator\Desktop\Demo.txt'
    User1
    User2
    User3


    PS C:\WINDOWS\system32>

    after that ..

    PS C:\WINDOWS\system32> $User = Get-Content 'C:\Users\Administrator\Desktop\Demo.txt'
    PS C:\WINDOWS\system32> $User
    User1
    User2
    User3
    PS C:\WINDOWS\system32>

    Working Fine ....

    But if I update that Demo.txt file, it would not replicate my variable .

    Is that meant to work like that or am i missing something ?

    Thursday, March 8, 2018 9:58 AM

All replies

  • If you update the file then you need to run the Get-Content command again for the variable to be updated.

    $ContentOutput = Get-Content 'c:\Users\Administrator\Desktop\Demo.txt'

    $ContentOutput



    • Proposed as answer by I.T Delinquent Thursday, March 8, 2018 10:30 AM
    • Unproposed as answer by jrv Friday, March 9, 2018 9:07 AM
    • Edited by I.T Delinquent Friday, March 9, 2018 9:27 AM Uuuuuge mistake
    Thursday, March 8, 2018 10:00 AM
  • I tried with first command nothing happen but if i Re assign the variable again ( second command) it works.

    Thursday, March 8, 2018 10:28 AM
  • In the next version of computers we will using quantum computing to allow variables to know when a remote file is being changed.  I hear they can do this with quantum particles.  A particle always knows what its twin I up to.


    \_(ツ)_/


    • Edited by jrv Saturday, March 10, 2018 6:49 PM
    Thursday, March 8, 2018 10:22 PM
  • I think you're a little confused. The first command "$user = get-content #PATH" put the value into the variable and the second line "$user" simply writes the value of the variable to the console.
    Friday, March 9, 2018 9:05 AM
  • If you update the file then you need to run the Get-Content command again for the variable to be updated.

    $ContentOutput = Get-Content 'c:\Users\Administrator\Desktop\Demo.txt' | Write-Host

    This command does nothing.  You cannot use Write-Host in a pipeline as it kills the pipeline.


    \_(ツ)_/

    Friday, March 9, 2018 9:08 AM
  • In the next version of computers we will using quantum computing to allow variables to know when a remote file is being changed.  I here they can do this with quantum particles.  A particle always knows what its twin I up to.


    \_(ツ)_/


    😉

    Regards kvprasoon

    Saturday, March 10, 2018 6:39 PM