ExpandProperty Expression

Welcome Forums General PowerShell Q&A ExpandProperty Expression

Viewing 5 reply threads
  • Author
    Posts
    • #240623
      Participant
      Topics: 11
      Replies: 13
      Points: 77
      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: 5
      Replies: 2413
      Points: 6,202
      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.

      $Val =
      Get-ChildItem -Path C:\Users -Exclude Default, Public |
      Where-Object { $_.LastWritetime -gt '1/1/2020' } |
      Select-Object -Property Name, @{Name = 'LastWriteTime'; Expression = { $_.LastWritetime.Tostring('D') } }

      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: 11
      Replies: 13
      Points: 77
      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: 4
      Replies: 480
      Points: 1,748
      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?

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

      Output looked like this in my temp directory.

      Sample2.csv                                  Friday, June 26, 2020      
      Sample3.csv                                  Thursday, June 25, 2020    
      Sample4.csv                                  Thursday, June 25, 2020    
      Sample5.csv                                  Friday, June 26, 2020      
      Sample6.csv                                  Friday, June 26, 2020      
      Sample7.csv                                  Friday, June 26, 2020      
      Sample8.csv                                  Friday, June 26, 2020      
      Sample9.csv                                  Friday, June 26, 2020      
      speech.ps1                                   Wednesday, July 1, 2020    
      states.txt                                   Wednesday, June 3, 2020    
      statesclean.txt                              Wednesday, June 3, 2020    
      synctest.ps1                                 Friday, May 1, 2020        
      synctest.ps1.old                             Friday, May 1, 2020        
      synctest1.ps1                                Friday, May 1, 2020     
      
    • #240659
      Participant
      Topics: 11
      Replies: 13
      Points: 77
      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: 4
      Replies: 480
      Points: 1,748
      Helping Hand
      Rank: Community Hero

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

      $Val = Get-ChildItem -Path C:\temp -Exclude Default,Public | ? {$_.LastWritetime -gt ‘1/1/2020’} |
          Select Name, @{Name = “LastWriteTime”; Expression={$_.LastWritetime.Tostring(“D”)}} | Out-String
      
Viewing 5 reply threads
  • You must be logged in to reply to this topic.