0

I have data in cell A2 of a CSV file (no headers) that looks like this: RPT DATE :07/20/23

How can I use PowerShell to read this cell from the file, strip out all except the date value and reformat it to look like: YY-MM-DD

I don't need to read any other data out of the file and there are no headers but there is other data in the first row of the file. I'm not able to modify the source CSV.

I've played with variations of the examples here with no luck: get specific cell value in csv file using powershell

This returns "WARNING: One or more headers were not specified. Default names starting with "H" have been used in place of any missing headers." but no actual value.

2
  • 2
    Please post the exact code that generates the warning :) Commented Jul 25, 2023 at 20:14
  • 1
    Welcome to Stack Overflow. Please take the 2-minute tour. Moreover, open Help center and read at least How to Ask. Then, edit your question to provide a minimal reproducible example. Commented Jul 25, 2023 at 20:21

2 Answers 2

0

you can use the Import-Csv cmdlet along with the -Header parameter to specify header names. Since your CSV file doesn't have headers, you can provide dummy header names. Then, you can access the cell value by referencing the corresponding header and row.

Here's how you can do it:

# Define the path to the CSV file and dummy header name
$csvFilePath = "path\to\your\file.csv"
$headerName = "Header"

# Import the CSV file with the dummy header

$data = Import-Csv -Path $csvFilePath -Header $headerName

# Get the value from cell A2

$value = $data | Where-Object { $_.PSObject.Properties.Name -eq 
$headerName } | Select-Object -ExpandProperty $headerName

# Extract the date from the value (assuming it's always at the end of the string)

$date = $value.Substring($value.LastIndexOf(":") + 2).Trim()

# Reformat the date to YY-MM-DD

$formattedDate = Get-Date $date -Format "yy-MM-dd"


# Output the result

Write-Output $formattedDate
Sign up to request clarification or add additional context in comments.

4 Comments

Is this from ChatGPT?
No, I did something similar a few months ago.
Thank you, this pointed me in the right direction
0

I wasn't able to get @RarikmilkraiSouza script working but it did help me figure it out where I needed to go. This is what I ended up with and it does what I need:

# Define the path to the CSV file and dummy header name
$csvFilePath = "C:\testfile.csv"
$headerName = "ColumnA"

# Import the CSV file with the dummy header

$data = Import-Csv -Path $csvFilePath -Header $headerName

# Get the value from cell A2

$value = $data[1].'ColumnA'

# Extract the date from the value and reformat to YY-MM-DD

$rptdate = $value -replace 'RPT Date :(\d{2})/(\d{2})/(\d{2})','$3-$1-$2'
$formattedDate = "20" + $rptdate

# Output the result

Write-Output $formattedDate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.