# Powershell script not working properly

**URL:** <https://forums.powershell.org/t/powershell-script-not-working-properly/13292>\
**Category:** PowerShell Help\
**Created:** [October 16, 2019, 4:08am UTC](https://forums.powershell.org/t/powershell-script-not-working-properly/13292 "2019-10-16T04:08:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kamesh](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kamesh](https://forums.powershell.org/u/kamesh)\
**Post date:** [October 16, 2019, 4:08am UTC](https://forums.powershell.org/t/powershell-script-not-working-properly/13292/1 "2019-10-16T04:08:12Z")

</div>

Hi Team I have powershell script that renames an existing file to a new file with appending sysdate, I am using the following script for this, this is called as part of another batch file, the file renaming is working properly but the sysdate is not appending properly for example today is 16th october but the file is renamed with 14th ocotober

```
function RenameFile
(
[string]$source_dir,
[string]$source_file,
[string]$source_filedate,
[string]$target_dir,
[string]$target_prefix,
[string]$target_suffix
)
{
$source_file_name = get-ChildItem $source_dir -recurse | where {$_.name -like $source_file.replace("<date>",$source_filedate)} | select -exp name
if ([string]::IsNullOrEmpty($source_file_name))
{
throw 'File Missing: ' + $source_file

}
else
{
$source_file = $source_dir + $source_file_name
$file = Get-Item $source_file

$a = Get-Date -format ddMMyyyy
$target_file = $target_dir + $target_suffix+"_"+"$a"+$target_prefix
Copy-Item $source_file $target_file
}
}

$source_directory = "E:\CSBE\output_files\DUEDIL\BE\FTP\"
$target_directory = "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO"

Remove-Item "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO\temp\*" -Force -Recurse

$todays_date = Get-Date -format "yyyyMMdd"
RenameFile -source_dir $source_directory -source_file BE_NEGATIVE_INFORMATION_<date>_D.dsv -source_filedate $todays_date -target_dir $target_directory INSOLVENCY_BE -target_prefix.dsv

$target_directory_temp = "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO\temp\"
RenameFile -source_dir $source_directory -source_file BE_NEGATIVE_INFORMATION_<date>_D.dsv -source_filedate $todays_date -target_dir $target_directory_temp INSOLVENCY_BE -target_prefix.dsv

cd "E:\CSBE\output_files\DUEDIL\BE\FTP\SP_NEGINFO\temp\"

dir "*.dsv" | ForEach-Object { & "\\Csgimp19\csbe2\bin\7Z.exe" a -tzip ($_.BaseName+".zip") $_.Name }
```

---

<div class="post-metadata">

**Author:** ![kpatnayakuni](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kpatnayakuni/32/19_2.png) [@kpatnayakuni](https://forums.powershell.org/u/kpatnayakuni)\
**Post date:** [October 16, 2019, 5:41am UTC](https://forums.powershell.org/t/powershell-script-not-working-properly/13292/2 "2019-10-16T05:41:09Z")

</div>

I don’t see anything odd there, add verbose statements and run it again with -Verbose.

Thank you.

---

<div class="post-metadata">

**Author:** ![an0nemus](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@an0nemus](https://forums.powershell.org/u/an0nemus)\
**Post date:** [October 16, 2019, 9:46am UTC](https://forums.powershell.org/t/powershell-script-not-working-properly/13292/3 "2019-10-16T09:46:02Z")

</div>

I don’t see any obvious flaws so I would look at a few basic items:

1. Is the system date on the server running the script correct?
2. How long was this running? (is it possible the script has been running since 10/14? )
3. Has it ever worked properly?
4. If you run it manually does it work?

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/powershell-script-not-working-properly/13292/4 "2024-05-16T20:31:16Z")

</div>

Closed on May 16, 2024, 8:31pm UTC


