Want to automate the Powershell Process

nupurs25

New Member
Joined
Apr 4, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi

I want to automate the process of Powershell where I write a code

import-csv C:\Users\Desktop\abcd\Transfers\xyz_Transfers_20230515.csv | export-csv C:\Users\Desktop\abcd\Transfers\xyz_Transfers_20230515_1.csv -NoTypeInformation -Encoding UTF8

I need to run this manually for a large number of files where every time the date is changing to a new one based on one less than current date and on monday last friday date.

I am writing this code to automate the process:

# Get the current date
$currentDate = Get-Date

# Determine the date based on the logic
if ($currentDate.DayOfWeek -eq 'Monday') {
$targetDate = $currentDate.AddDays(-3)
} else {
$targetDate = $currentDate.AddDays(-1)
}

# Format the target date as 'yyyyMMdd'
$targetDateString = $targetDate.ToString('yyyyMMdd')

# Define the source and destination paths
$sourcePath = "C:\Users\Desktop\abcd\Transfers\xyz_Transfers_$targetDateString.csv"
$destinationPath = "C:\Users\Desktop\abcd\Transfers\xyz_Transfers_$targetDateString_1.csv"

# Import the CSV file and export with a modified filename
Import-Csv $sourcePath |
Export-Csv $destinationPath -NoTypeInformation -Encoding UTF8

Write-Host "CSV file processing completed for $targetDateString."

This creates a new files with the previous date but I want to create a file with date as the one in my source file. Say my source file name is ABC_XYZ_Transfer_20230531, then it should generate the new file as ABC_XYZ_Transfer_20230531_1 and not ABC_XYZ_Transfer_20230602 which is the one generating if I run the code. Also, when I open the file generated it just gives me the code but not the data in the original destination file. Why is that so? Sorry if misunderstood current date meant the date as per the one in the source file not the real "current" date.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
$currentDate = Get-Date should be the date that is in the file and is named as for example ABC_XYZ_20230601. So I want this file date to be captured in the $current date. This file is the source path file and needs to generate the destinantion path file and use the current date as per the source path file.

I feel making changes to the $currentdate part of formuale will solve this issue.
 
Upvote 0
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,004
Members
449,203
Latest member
Daymo66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top