Sunday, November 20, 2022

Why and How to use PowerShell script in CMD Windows?

Why ?

If you intended to use a PowerShell functions and code to retrieve certain output or do some work using PowerShell scripting instead of CMD and accordingly you will complete your work in CMD batch scripting , in this case I am sharing a simple code to achieve this.

How ?

Below is a sample code script running as .bat , CMD batch scripting that will call PowerShell script to retrieve yesterday date and store it in CMD batch variable and continue work with it in CMD , retrieving yesterday date is very difficult in CMD however it is so easy on PowerShell thus I use it as below:

1- Code below, please save it in .bat and run it using CMD:

for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%m%%d"') do set date=%%a

for /f "delims=" %%a in ('powershell get-date((get-date^).addDays(-1^)^) -uformat "%%Y%%b%%d"') do set date2=%%a

  

echo %date%

echo %date2%

 

::echo %datetime%

set year=%date:~0,4%

set mm=%date:~4,2%

set day=%date:~6,2%

set mmm=%date2:~4,3%

 

echo checking files for date %year%/%mm%/%day% %time%

echo checking files for date %year%/%mmm%/%day% %time%

pause  

No comments:

Post a Comment

Why and How to install Grid 19c on RHEL 8?

  Why ? Simply we will be requested to install Oracle Grid RAC DB on Redhat RHEL 8, below is my note for this installation . How ? 1-  OS in...