ExpandProperty Expression

Welcome Forums General PowerShell Q&A ExpandProperty Expression

Viewing 5 reply threads
  • Author
    Posts
    • #240623
      Participant
      Topics: 12
      Replies: 14
      Points: 92
      Rank: Member

      Hello All,

      Am trying to use the ExpandPropery against an expression, but it is throwing up some error

      $Val = Get-ChildItem -Path C:\Users -Exclude Default,Public | ? {$_.LastWritetime -gt ‘1/1/2020’} | Select Name, @{Name = “LastWriteTime”; Expression={$_.LastWritetime.Tostring(“D”)}} -ExpandProperty Name ,LastWriteTime

      Would like to store the values under $val. What am i doing wrong here ?

    • #240626
      Participant
      Topics: 10
      Replies: 2481
      Points: 6,543
      Helping Hand
      Rank: Community MVP

      Vinod,

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “CODE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      It doesn’t make any sense to select properties twice in one Select-Object.

      When you get errors you should always post the complete error message along with your question. Most of the time the error message points to the solution. 😉

    • #240629
      Participant
      Topics: 12
      Replies: 14
      Points: 92
      Rank: Member

      Thanks. So i need $Val to have both values like the username and date. how can it be done.

      The error is

      Select-Object : Cannot convert ‘System.Object[]’ to the type ‘System.String’ required by parameter ‘ExpandProperty’.
      Specified method is not supported.

    • #240656
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      I don’t know where username came into this, but if you simply drop the -expandproperty it’s probably what you’re looking for?

      Output looked like this in my temp directory.

    • #240659
      Participant
      Topics: 12
      Replies: 14
      Points: 92
      Rank: Member

      Thanks. I am trying to write the output to a Textbox inside the form wherein it shows in wrong format

      $TextBox1 = New-Object system.Windows.Forms.TextBox
      $TextBox1.multiline = $true
      $TextBox1.WordWrap = $true
      $TextBox1.width = 400
      $TextBox1.height = 200
      $TextBox1.location = New-Object System.Drawing.Point(20,59)
      $TextBox1.Lines = $Val

      The output i get it when the form is launched

       

      @{Name=ABCD; LastWriteTime=22 June 2020}
      @{Name=SER; LastWriteTime=05 May 2020}
      @{Name=Sample; LastWriteTime=05 May 2020}
      @{Name=XYX; LastWriteTime=06 March 2020}
      @{Name=RTYT; LastWriteTime=05 March 2020}
      @{Name=UIOP; LastWriteTime=28 January 2020}

      How do we remove the Name and Lastwrite so that it displays only those values

    • #240665
      Participant
      Topics: 9
      Replies: 707
      Points: 2,848
      Helping Hand
      Rank: Community Hero

      Then you would want it to be a string, not an object.

Viewing 5 reply threads
  • The topic ‘ExpandProperty Expression’ is closed to new replies.