1

I have written a PowerShell line directly in PowerShell and it works perfectly but I can't get the .bat to run it.

I think my PowerShell script running is blocked by corporate.

If I can get the same result in CMD then it will solve that issue.

Here is the PowerShell code:

CD C:\Temp

Get-ChildItem -Filter "*-Layout1*" -Recurse | Rename-Item -NewName {$_.name -replace ‘-Layout1’,'' }

This code takes PLANVIEW-Layout1.pdf and turns it into PLANVIEW.pdf but it will take any file name with -Layout1 and remove the -Layout1.

My CAD programs create .pdf's while adding "-Layout1" or "-Model1" to the file name.

I sometimes create 100's of PDF's and renaming all of them one by one is a pain.

The PowerShell code above runs perfectly if I type it in the PowerShell window.

I want other users to be able to use this as well without having to type all the code each time.

EDIT: Sorry for the messy comments... When I run this .bat:
@echo off PowerShell.exe -noexit -ExecutionPolicy Bypass -File "MyScript.ps1" Pause
with MyScript.ps1:
CD C:\Temp Get-ChildItem -Filter "*-Layout1*" -Recurse | Rename-Item -NewName {$_.name -replace ‘-Layout1’,'' }

I now get this in the CMD:

You must provide a value expression on the right-hand side of the '-replace' operator. At C:\Temp\MyScript.ps1:3 char:85 * Get-ChildItem -Filter "-Layout1" -Recurse | Rename-Item -NewName {$_.name -replace <<<< a?~-Layout1a?T,'' } + CategoryInfo :ParserError: (:) []. ParentContainsErrorRecordException + FullyQualifiedErrorId :ExpectedValueExpression

  • Check the ExecutionPolicy, technet.microsoft.com/en-us/library/ee176961.aspx . You can also save and execute your line through a .ps1. – root Mar 11 '16 at 13:34
  • Re-reading your initial post, I see that "corporate" may have blocked something. SuperUser generally does not allow topics related to corporate networks. – root Mar 11 '16 at 13:43
  • I think it is running script to PowerShell. not using .bat in general or not using PowerShell in general. I have approval to try to create the script I am trying to create I just can't get the automation part. I have the working code and can run it in my PowerShell. If I can get it to run in CMD instead then I wont have to worry about the ExecutionPolicy. It's just that PowerShell & CMD are different languages. – Christopher Mar 11 '16 at 13:48
  • Please do not post code snippet(s) and huge error messages in comment. Use edit link instead to improve your question. – JosefZ Mar 11 '16 at 16:59
0

Regarding the error output and the ps1 code, it seems that the " ‘ " quote around ‘-Layout1’ are not recognized well.

You may try to remplace them by simple quotes like that :

Get-ChildItem -Filter "*-Layout1*" -Recurse | Rename-Item -NewName {$_.name -replace '-Layout1','' }
  • That was it. I can't believe it was that simple. The PowerShell code typed directly into PowerShell works but it needed to be modified to be ran from the batch file. I have soo much to learn. Thank you so much for your help! – Christopher Mar 11 '16 at 18:54
  • The strangest thing.... I went back to test it putting the ' back in and that works now as well. I must be going crazy. But I will keep it how you suggested and run from there. Thanks again. I appreciate all of your patients. – Christopher Mar 11 '16 at 19:09
  • Well, if the code in the question block is literal, then it can be seen that those were not apostrophes but backticks. They are similar to the eye but are quite different characters... just like minus signs and text dashes which often cause problems in codes written with text processors thinking always in unicode instead of text editors. – Gombai Sándor Mar 11 '16 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.