I would like to find every instance of "blue" and change it to "pink" in multiple text files. I cannot download FAR (Find And Replace) softwares so I need to use what is already on the computer: Powershell/cmd/batch.
Not knowing how to do this for multiple files in Powershell, I decided to combine batch and Powershell. This is the batch code I currently have:
for %%F in ("C:\mypath\*") do (
powershell -command(get-content %%F) -replace 'blue', 'pink'| set-content -encoding ASCII %%F
)
This does not work, I receive the error message "set-content is not recognized as an internal command".
I realize that using two languages together can lead to some issues, so is this doable exclusively with batch or exclusively with Powershell?
Thank you.
This is the completed code. It finds "blue" and replaces it with "pink" through multiple files, with a path that includes spaces:
for %%F in ("C:\mypath\*") do powershell -command "(get-content '%%F') -replace 'blue', 'pink'| set-content -encoding ASCII '%%F'"
Thanks to Aacini, Squashman and Compo for their help.
You should using only PowerShell if you can :
Get-ChildItem "C:\mypath" -file | %{$path=$_.FullName; (Get-Content $path).Replace('blue', 'pink') | set-content $path -encoding ASCII }
Related
In Windows, I want to remove white spaces from file names and change the lower case to upper case.
file 1.txt --> FILE1.TXT
File 2.txt --> FILE2.TXT
Test 1.txt --> TEST1.TXT
I tried something like this (which is not working) on command prompt
rename "*.txt" "*.TXT" # Works
rename "file*.txt" "FILE*.TXT"
rename "Test*.txt" "TEST*.TXT"
As #Mofi said, cmd is not well equipped to do this. If you are on a supported Windows system, powershell was installed with it. When you are confident that the files would be renamed as you expect, remove the -WhatIf from the Rename-Item command.
Get-ChildItem -File | Rename-Item -NewName { $_.Name.ToUpper() } -WhatIf
If you are desperate to run this from a cmd prompt or batch-file, this could be used.
powershell -NoLogo -NoProfile -Command ^
Get-ChildItem -File ^| Rename-Item -NewName { $_.Name.ToUpper() } -WhatIf
good day, i was searching all over the internet for a powershell command/script that can search and delete any folder named (Games*) (game*) on remote machines
on CMD i used to command "del /s \computername\c$\Game*" and it works perfectly with me however it delete files only, need a powershell script that can delete files and folders.
Make use of pipeline option in powershell .Below code will do the work.
get-childitem -path 'FilePath' -Recurse -Filter "Game*" | remove-item -Force
Hope it HElps.
Remove-Item \\compname1\c$\Game* -force -recurse
I want to run a few PowerShell commands through a batch file. Very similar question has been asked but I dont want to run a seperate shell file from a batch. Instead I want to embed PowerShell commands to a batch file.
When I try to run
powershell -command "&{$var = "something"}"
I get the following error:
something : The term 'something' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:10
&{$var = something}
CategoryInfo : ObjectNotFound: (something:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
But if I run something like
powershell -command "&{echo "something"}"
Then everything is fine :/
Am I doing a syntax error or something? And please don't give answers like "Instead of using PowerShell commands use batch commands etc..."
Thanks in advance!
You can not use nested quotes when defining the value of a variable as a string. You may either use apostrophes instead of the nested quotes:
powershell -command "&{$var = 'something'; echo $var}"
... or escape the nested quotes:
powershell -command "&{$var = \"something\"; echo $var}"
Another example:
powershell -command "&{$var = 'one'+\" two\"; echo $var}"
Maybe a bit late but I program pure powershell scripts and embed the complete script in a .cmd file to bypass the execution policy setting. I have 2 variants pick the one you like. Both are one-liners that you put on the first line of the .cmd file. Starting from the second line you just program in pure powershell. No need for modifying anything in your powershell script.
Variant1:
#type "%0" | findstr /v "^#type \"%0\" | findstr /v " | PowerShell.exe -noprofile - & goto :eof
Varant2:
#powershell -command "(Get-Content '%0') | select -skip 1 " | powershell -noprofile - & goto :eof
I need to write a script .bat that give me the free space of a certain disk and the detail of all folders with the relative dimension and write it in a simple text file (just to check the distribution of used space).
Thanks a lot in advance!
dir /a /s | findstr /b /c:" " > file.txt
The FileSystemObject can do this for you. It is accessible from PowerShell (among other sources).
PowerShell -NoProfile -Command "$fso = New-Object -COMObject Scripting.FileSystemObject; Get-ChildItem YOUR_ROOT_DIRECTORY -Recurse -Directory | %% { $f = $fso.GetFolder($_.FullName); '{0},{1}' -f $f.Size,$_.FullName };"
I have a text file of the format:
computername1 uninstallkey1
computername2 uninstallkey2
...
computername200 uninstallkey200
I am trying to write a startup script (batch file or powershell?) that generates a msiexec command that looks up and implants the correct key for each computer it executes on e.g.:
msiexec /x install.msi key=uninstallkey
If I have not made anything clear, please ask and any help is much appreciated!
#ECHO OFF
SETLOCAL
FOR /f "tokens=1*" %%i IN (yourtextfilename.txt) DO (
IF /i %%i==%COMPUTERNAME% ECHO MSIEXEC /x install.msi key=%%j
)
This should do as you require - yourtextfilename.txt contains the data, presumably on a shared drive; finds the line where the computername in column 1 is the same as the computername returned by %computername% in the target computer's environment.
(all case-insensitive EXCEPT %%i and %%j which must match and be the same case)
Command simply ECHOed - remove the ECHO keyword after verification to activate.
In PowerShell,
$comp = Import-CSV -Delimiter " " -Path C:\comp.txt -Header computername,uninstallkey
$comp | ForEach-Object {
if ($env:COMPUTERNAME -eq $_.Computername) {
Start-Process -FilePath "msiexec.exe" -ArgumentList "/x install.msi key=$_.uninstallkey"
}
}