POwershell script giving 0 bytes OUt-file

Hi,

I have a file like below

2020-02-29 00:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 314,
2020-03-01 00:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 264,
2020-03-01 01:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 29,
2020-03-01 01:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 11,

I am using below script to get last 4 hours data, every 4 hours. I was working until last week, but its not now. I am not sure what the issue is.

$Data = Import-CSV -path “E:\Chayan\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_1.csv” -Header “Date”,“Col2”,“Col3”,“Col4”
$CutoffDate = (Get-Date).AddHours(-4)
$Data | Where-Object {$_.Date -as [datetime] -gt $CutoffDate} |
Export-Csv -Path “.\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_2.csv” -NoTypeInformation

 

It’s working. None of the dates above is from the last 4 hours.

Yes, sorry for posting such a silly question. I should have looked at the source stack, one more issue though. how do I remove the column headers from this file. I am using -skip 1, but it doesn’t seem to be working.

 

col1 col3 col4


2020-03-01 00:52:40,ACE001P,QL.IN… 264,
2020-03-01 01:22:40,ACE001P,QL.IN… 29,

Export-Csv puts column headers in automatically. You could remove them after the file is written or not use export-csv and use Add-Content to write plain text to the file. Here is an example of how you could remove the header row after the file is written:

$Data = Get-Content -Path .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_2.csv 
$Data[1..($Data.Count)] | Set-Content .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_2.csv

Here’s an example of how you could change from Export-Csv to using Set-Content

$Data | ForEach-Object {
$Value = $_.Date, $_.Col2, $_.Col3, $_.Col4
Add-Content -Path .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE001P_3.csv -Value ($Value -join ",") }

 

This is from a different command I am using to extract specific columns from a source stack. Here is the command.

Import-Csv .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE002P.csv -Header col1,col2,col3,col4,col5,col6,col7,col8,col9,col10 -Delimiter ’ ’ | Select-Object col1,col3,col4|Out-File .\QL.IN.XCENTER.ERECEIPT.FACADE_ACE002P_2_1.csv

 

2020-02-29 23:52:40,2020-03-01 00:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 314, 314, 0, 0, 2

2020-03-01 00:22:40,2020-03-01 00:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 264, 264, 0, 0, 1

2020-03-01 00:52:40,2020-03-01 01:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 29, 29, 0, 0, 1

2020-03-01 01:22:40,2020-03-01 01:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 11, 11, 0, 0, 1

2020-03-01 01:52:40,2020-03-01 02:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 14, 14, 0, 0, 1

2020-03-01 02:22:40,2020-03-01 02:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 8, 8, 0, 0, 1

2020-03-01 02:52:40,2020-03-01 03:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 1, 1, 0, 0, 1

2020-03-01 09:22:40,2020-03-01 09:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 4, 4, 0, 0, 1

2020-03-01 09:52:40,2020-03-01 10:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 76, 76, 0, 0, 1

2020-03-01 10:22:40,2020-03-01 10:52:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 324, 324, 0, 0, 2

2020-03-01 10:52:40,2020-03-01 11:22:40,ACE001P,QL.IN.XCENTER.ERECEIPT.FACADE, 692, 692, 0, 0, 2

 

I want this to output without the following column headers.

col1 col3 col4


2020-02-29 00:22:40,ACE001P,QL.IN… 314,

2020-03-01 00:52:40,ACE001P,QL.IN… 264,

2020-03-01 01:22:40,ACE001P,QL.IN… 29,

If you look at @MikeR’s response, it explains how to do this