This code unblocks a OneDrive library, for the sake of security. The library is found in the C:\ODTool directory, and is downloaded form a GitHub online open project. It uses PowerShell to import a module, then run the command Get-ODStatus, to determine the sync status of OneDrive, outputting it to output.csv in the current directory.
PS C:\ODTool> Unblock-File -Path C:\ODTool\OneDriveLib.dll
Import-Module C:\ODTool\OneDriveLib.dll
Get-ODStatus > output.csv 2>&1
I need to convert this PowerShell script to an executable batch file command.
Create a txt file and add the following code:
#ECHO OFF
Powershell.exe -executionpolicy bypass -File <full path to ps1>
then save the file as a .bat
Related
I have this simple PowerShell script:
Copy-Item -path ('\\PC1\Images\' + (Get-ChildItem -Path '\\PC1\Images' -Attributes !Directory *.dat | Sort-Object -Descending -Property LastWriteTime | Select-Object -first 1)) 'C:\Temp\PC1\screen.jpg' | Start-Process 'C:\Program Files\Google\Chrome\Application\chrome.exe' -ArgumentList 'file:///C:/Temp/PC1/screen.jpg'
If I run this command from a PowerShell window, it works fine.
If I save it as .ps1 file and run it from Windows Explorer, nothing happens (Chrome does not open the desired URL).
What can be done to allow to execute this script from some shortcut? (I want to add a shortcut/icon to my Windows taskbar for fast access).
You have two options:
Either: Make .ps1 files themselves - which by default are opened for editing when opened (double-clicked) from File Explorer / the desktop / the taskbar - directly executable:
See this answer.
Important: Doing this will make all .ps1 files you drag to the taskbar be associated with a single icon, for PowerShell itself, with the dragged files getting pinned to that icon; that is, to run such a file you'll have to do so via the PowerShell icon's shortcut menu.
Or: For a given .ps1 file, create a companion shortcut file (.lnk) or batch file (.cmd) that launches it, via PowerShell's CLI (powershell.exe for Windows PowerShell, pwsh.exe for PowerShell (Core) 7+)
Note: Once you've created a companion file as described below, dragging it to the taskbar will give it is own taskbar icon.
Batch-file approach:
Create a companion batch file in the same folder as your .ps1, with the same base file name; e.g., foo.cmd alongside foo.ps1
Add the following content to the .cmd file (add -NoProfile and -ExecutionPolicy Bypass as needed):
#powershell.exe -NoExit -File "%~dpn0.ps1" %*
Shortcut-file approach:
See this answer for how to create such a shortcut file interactively.
See this answer for how to create shortcut files programmatically.
What is the output of running next command in Powershell?
Get-ExecutionPolicy
If the output you get is "Restricted", you can try creating a .cmd file situated in the same folder as the .ps1 file with the next content:
PowerShell.exe -ExecutionPolicy Bypass -File "your_ps1_name.ps1"
I wanted to use Windows built-in table viewer to open CSV file, like this SO answer shows.
So I want that when I double click on a CSV file in Explorer, the following command is run:
Import-Csv [CSV-FILE] |Out-GridView
I ended up creating two script files. One file name "read_csv.bat" which contains:
powershell.exe -ExecutionPolicy ByPass -noexit -File %~dp0read_csv.ps1 -csvfile %1
pause
Another file name "read_csv.ps1" which contains the actual script
param (
[Parameter(Mandatory=$true)][string]$csvfile
)
Import-Csv $csvfile |Out-GridView
Is there now way to do it more efficiently, so with only one script file? If I set explorer to open the CSV file with the POwerShell script directly, a blue message appears
This app can't run on your PC
To find a version for your PC, check with your published
Note:
Since powershell.exe is ultimately called, a console window will invariably (also) open when a CSV file is opened, because powershell.exe is a console-subsystem application.
Providing an alternative, GUI-subsystem executable to avoid creating a console window is the subject of this feature request on GitHub; in the meantime, there are workarounds:
A VBScript-based solution is shown in this answer.
A script-less, but complex and potentially AV-software-triggering alternative is used in this answer.
You'll need to use an adapted version of the powershell.exe command from your batch file, because *.ps1 files are by design not directly executable.
That is, in the registry definition for the CSV file type, use the following command to define the action for the Open verb (see below):
powershell.exe -ExecutionPolicy ByPass -NoExit -File c:\path\to\read_csv.ps1 -csvfile "%1"
Be sure to substitute the real path to your *.ps1 script for c:\path\to\read_csv.ps1 (double-quote it, if necessary); you can either use a literal path, or one based on cmd.exe-style environment-variable references (e.g., "%USERPROFILE%\read_csv.ps1").
Alternatively, you can make do without a script file altogether, using the PowerShell CLI's -Command parameter:
powershell.exe -ExecutionPolicy ByPass -NoExit -Command Import-Csv \"%1\" | Out-GridView
To automate the process of configuring this command for opening CSV files via File Explorer / the desktop:
The code below modifies the registry as follows:
defines a new file type, PsCsvViewer, with an Open verb (operation) that invokes the PowerShell command defined above.
associates the .csv filename extension with that new file type.
Note: The first time you open a CSV file after the redefinition, you may be prompted to confirm the intent to use a PowerShell command from now on.
creates the definitions above for the current user only, which means that you don't need admin privileges to run the code (which writing to subkeys of HKEY_CLASSES_ROOT\ would require).
# Note the need for *3* "\", because an extra layer of escaping is
# needed for reg.exe.
$cmd = 'powershell.exe -ExecutionPolicy ByPass -NoExit -Command Import-Csv \\\"%1\\\" | Out-GridView'
# Create a new file type for the PowerShell command.
reg.exe add HKCU\Software\Classes\PsCsvViewer\Shell\Open\command /ve /d $cmd /f
# Associate .csv files with the new file type.
reg.exe add HKCU\Software\Classes\.csv /ve /d PsCsvViewer /f
Trying to make a .bat I can drop in a folder, when run it will unblock all the files in that folder...
#ECHO OFF
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {get-childitem '%~dp0' | unblock-file}"
EXIT
...keeps telling me "The term 'unblock-file' is not recognized as the name of a cmdlet..." no matter how I try to format it, where am I going wrong?
I'm trying to do this so I can just copy the .bat to the folder (and NOT have to copy a .bat and .ps1) so I thought a 1-line powershell "call" was the way to go?
The unblock-file command is available from Powershell 3.0.
Upgrade your PowerShell and script should work
Tested and working:
dir -r | unblock-file
Unblocks everything from the current directory recursively
I am fairly new to powershell and I am trying to create a script that executes a .exe file. I can execute them on my machine no problem because the folder path is hard coded. The problem is that if I shift this script to another computer, the .exe it calls might be located in a different folder structure. Example
My computer:
D:\Folder1\subfolder\RunMe.exe
Client computer might be
D:\RunMe\subfolder\RunMe.exe
I just need it to execute the RunMe.exe no matter where it is. Is there a way to do this in powershell?
# 1. Get the location of RunMe.exe
$RunMe = Get-ChildItem -Path d:\* -Include RunMe.exe -Recurse;
# 2. Invoke RunMe.exe
Start-Process -FilePath $RunMe[0].FullName -Wait -NoNewWindow;
I've been trying to launch a simple powershell script from a batch file. After looking online the advice is to set the policy using Set-ExecutionPolicy.
I've done this and using Get-ExecutionPolicy the policy has been changed.
However running this batch file results in an 'Open With' dialog rather than the execution of the script.
Batch:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe ^&'./script.psl'
Powershell:
Write-Host "This is a message"
I have sampled on both Windows 7 and Server 2008 R2. Both have same result. What am I missing?
To run a script file from the command line, use the -file parameter of powershell.exe.
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file './script.psl'
To run a script file from the *.cmd file , use the -file parameter of powershell.exe and double quotes:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -file "./script.ps1"
When you will use only one quote in batch file you can expect powershell error like:
Processing -File ''./build.ps1'' failed because the file does not have
a '.ps1' extension. Specify a valid Windows PowerShell script file
name, and then try again.