Sub Expression using Read Host

Welcome Forums General PowerShell Q&A Sub Expression using Read Host

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #81245

    Participant
    Points: 1
    Rank: Member

    Can someone please explain to me why this doesn't work?

    Get-Item "'\\$(Read-Host 'Specify Computer')\c$\Program Files (x86)\Microsoft Office\root\Office16\winword.exe'"
    "'\\$(Read-Host 'Specify Computer')\c$\Program Files (x86)\Microsoft Office\root\Office16\winword.exe'" | Get-Item

    Write-Host behaves exactly as I would expect...

    Write-Host "'\\$(Read-Host 'Specify Computer')\c$\Program Files (x86)\Microsoft Office\root\Office16\winword.exe'"
    "'\\$(Read-Host 'Specify Computer')\c$\Program Files (x86)\Microsoft Office\root\Office16\winword.exe'" | Write-Host

    Also, if I manually provide string it works fine.

    Get-Item "'\\computer\c$\Program Files (x86)\Microsoft Office\root\Office16\winword.exe'"
    "'\\computer\c$\Program Files (x86)\Microsoft Office\root\Office16\winword.exe'" | Get-Item

    Any idea as to why Get-Item wants to append the target location to the current location in this circumstance?

    Thanks in Advance

  • #81253

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    The C$ should probably be C`$ to escape the dollar sign. I'm also not sure what the embedded single quotes are doing for you.

    What's this not doing that you expected it to?

    • #81256

      Participant
      Points: 1
      Rank: Member

      Removing extra set of quotes resolved issue. I swear initially had a single pair of quotations and it didn't like that because there were spaces in the path so that's why I added the second pair. In any case, it does what I want it to do now so I'm happy 🙂 Thank you sir!

The topic ‘Sub Expression using Read Host’ is closed to new replies.