Get rid of Name header in an array

This topic contains 4 replies, has 3 voices, and was last updated by  Rich TheH 3 years, 4 months ago.

  • Author
  • #22887

    Rich TheH

    I am trying to create an array of Excel file basenames, as a string, to run through a foreach loop later in my script. I was able to generate the array but it includes the header "name" and is causing the script to error out. I am sure I did this in a goofy and more complicated way than needed.

    $d = get-date -displayhint date
    $h = Get-ChildItem $connsource |
    Select-object -property name, lastwritetime |
    where-object {$_.LastWriteTime -ge ($d.adddays(-2))} | 
    Select-object -property name | out-string
    $table = $h.replace(".xlsx","")

    I want to be sure to only grab Excel files dated within the last 2 days. Not pretty but it works as far as stripping the file name down to the base name but I don't know how to exclude the "[u]Name[/u]" header when I call the $table variable. I think my issue is the last Select-Object before the out-string, but not sure and not sure how else to do this.

    Thanks in advance for any help.

  • #22890

    Kiran Reddy

    to exclude the name header you can use

    Select-object -ExpandProperty name | out-string

    also if you only want to retrieve excel files consider adding a filter to Get-Childitem

    get-childitem c:\temp -filter *.xlsx

  • #22891

    Craig Duff

    +1 to using -expandproperty
    There is a property called basename so there is no need to manually strip the xlsx
    There is no need for out-string because the property you are expanding is already a string.
    The first select-object is unnecessary

  • #22894

    Rich TheH

    Thanks Kiran and Craig. While driving home I thought about the -expandproperty. I will add that and test. I also realized that now i have one long string rather than a series of names so i will revise again. I did have a filter at one point but must have deleted while trouble shooting. Thanks for pointing that out.

  • #22902

    Rich TheH

    Thanks for sharing the knowledge. -expandproperty was the solution i needed to include.

You must be logged in to reply to this topic.