Powershell to unblock all the files in a folder - powershell

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

Related

How to convert PowerShell code for a batch file?

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

Batch file wait for powershell line to unzip before executing next command?

Basically I have a Batch file that use a one lined powershell command to unzip a file, but it doesn't wait for that powershell command to finish executing. Call doesn't work, start /wdoesn't work, start powershell [cmd] -Wait doesn't work What DO I DO?
cd..
start powershell Expand-Archive update.zip -DestinationPath %appdata%\ModManager\ > nul
cd %appdata%\ModManager\
[other code...]
Just don't use "start". Just run powershell directly. There are also a couple of things you need to fix in your batch file. Replace the 2nd line with this command instead:
powershell -executionpolicy unrestricted -command "Expand-Archive update.zip -DestinationPath $env:APPDATA\ModManager\ | out-null"

Is it possible to permanently change the Powershell working directory from a batch file?

I wanted to write this simple script as a proof of concept, but it's not behaving like I expected...
mkdir %1
cd %1
touch main.py
vim main.py
The new directory and file are created, and the file is opened in Vim properly, which means that the call to cd was successful, but the Powershell instance reverts back to the old directory when the script finishes execution. Is there any way to prevent this from happening?
I browsed through Stack Overflow and I found this.
powershell.exe -NoExit -command "& {Set-Location $env:systemroot}"
powershell.exe -NoExit -command "& {Set-Location "D:\path\path}"
Set this command to a Powershell shortcut, issue fixed(of course change $env to what you need)
Also, you could start your own script in the -command flag.
Ok, got it working. The solution was to run the script from a .ps1 file instead of .bat. Not sure why the .ps1 behaves differently, but I'll take it.
Param([string]$filepath)
mkdir $filepath
cd $filepath
touch main.py
vim main.py

how do I make it easy for my parents to run this Powershell command?

I am not a programmer and my parents' Windows 10 PC tends to loose its start menu and cortana processes, resulting in start menu not showing up at all when the start icon is clicked.
I made a quick search and found + tested this Powershell command and it worked:
Get-AppxPackage | % { Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppxManifest.xml" -verbose }
I wish to turn this command into a shortcut/batchfile that executes the command and restarts the PC whenever the desktop icon is double clicked, in order to avoid explaining to my parents what to do to fix the problem. Can any one help me out please?
Thank you in Advance.
you can encode the command and put the whole thing into a single batch file (no .ps1 necessary)
details here
https://blogs.msdn.microsoft.com/timid/2014/03/26/powershell-encodedcommand-and-round-trips/
or you can use this function
https://github.com/gangstanthony/PowerShell/blob/master/Encode-Text.ps1
first, either use Get-Content or Get-Clipboard (copy your whole script to the clipboard) to encode your desired script
PS> Encode-Text (Get-Clipboard | out-string)
RwBlAHQALQBBAHAAcAB4AFAAYQBjAGsAYQBnAGUAIAB8ACAAJQAgAHsAIABBAGQAZAAtAEEAcABwAHgAUABhAGMAawBhAGcAZQAgAC0ARABpAHMAYQBiAGwAZQBEAGUAdgBlAGwAbwBwAG0AZQBuAHQATQBvAGQAZQAgAC0AUgBlAGcAaQBzAHQAZQByACAAIgAkACgAJABfAC4ASQBuAHMAdABhAGwAbABMAG8AYwBhAHQAaQBvAG4AKQBcAEEAcABwAHgATQBhAG4AaQBmAGUAcwB0AC4AeABtAGwAIgAgAC0AdgBlAHIAYgBvAHMAZQAgAH0ADQAKAA==
then you can use that in your batch file like so
powershell -encodedcommand RwBlAHQALQBBAHAAcAB4AFAAYQBjAGsAYQBnAGUAIAB8ACAAJQAgAHsAIABBAGQAZAAtAEEAcABwAHgAUABhAGMAawBhAGcAZQAgAC0ARABpAHMAYQBiAGwAZQBEAGUAdgBlAGwAbwBwAG0AZQBuAHQATQBvAGQAZQAgAC0AUgBlAGcAaQBzAHQAZQByACAAIgAkACgAJABfAC4ASQBuAHMAdABhAGwAbABMAG8AYwBhAHQAaQBvAG4AKQBcAEEAcABwAHgATQBhAG4AaQBmAGUAcwB0AC4AeABtAGwAIgAgAC0AdgBlAHIAYgBvAHMAZQAgAH0ADQAKAA==
You could execute the PowerShell script via a batch file.
Batch file:
set powerscriptPath=C:\Example.ps1
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%powerscriptPath%""' -Verb RunAs}"
This will bypass the execution policies on the computer allowing the script to run in Administrator mode too. NOTE: You will need to edit the powerscriptPath to point to your PowerShell script location, I just used C:\Example.ps1 as an example.
You will want to add Restart-Computer -Force to the end of your PowerShell script to restart the computer
Get-AppxPackage | % { Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppxManifest.xml" -verbose }
Restart-Computer -Force
Make a bat file which executes powershell with that file. Then add a shortcut to the bat file
I am really unsure why you would run a batch file just to call a powershell script! Talk about hokey approaches to a non-problem.
To call a powershell script is really no different than calling a batch script:
It's simply path to PowerShell, and the script path as a parameter:
"%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" "C:\users\austinfrench\desktop\example.ps1"
You can also use the exact same format as the target for a desktop shortcut.

Powershell script not firing from batch file

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.