DOS/PS commands
windows cmd or powershell
Personal note
if you also have python experience, some helpful analogs are:
| python | PS(powershell) | |
| variables | var | $var |
| for loop | for i in ls: … | foreach($i in $ls){ … } or for($i=1;$i -le 10; $i++){echo @($i*2)} |
Access/Permission Management
Check if the Admin group:
Get-LocalGroupMember -Group “Administrators”
Or run “lusrmgr.msc” check groups
Ways to set permission:
old fashion way
$ACL = Get-Acl $Path
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($User, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
$ACL.SetAccessRule($AccessRule)
Set-Acl -Path $Path -AclObject $ACL
CLI tool
icacls.exe "./Data" /inheritance:r /grant:r "John":(OI)(CI)F /T /C
/inheritance:r -- disable legacy permissions
/grant:r -- overwrite (r) access
/grant -- add access
(I)(OI)(CI)(IO)(F) in icacls are (I) Inherited, (OI) Object Inherit (files), (CI) Container Inherit (folders), (IO) Inherit Only (does not apply to current folder), and (F) Full Access
/T: Recursively, apply to subfolders
/C: continue at errors
icacls "D:\Target" /reset
/reset: reset access
Note: this cannot be combined with /inheritance, they are the same level commands.
icacls "D:\Target" /grant Administrators:(OI)(CI)F
Ownership
Higher-level lockout
takeown /F D:\YourFolder /A /R /D Y
create softlink
New-Item -ItemType SymbolicLink -Path "C:\Users\Admin\Desktop\LinkedFolder" -Target "D:\LockedFolder"
This is fundamentally different from the shortcut in GUI. If you need a .ink for visual interaction, do the following in powershell
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("C:\Path\To\Shortcut.lnk")
$Shortcut.TargetPath = "D:\Actual\Folder"
$Shortcut.Save()
/F: folder directory
/A: grant ownership to admin group
/R: recursively
/D Y : set default answer to yes, mainly for subdirectory managing.
Batch run (jobs)
Below is example of batch run a python script in designated virtual environment as a parallelizable job. Note the variables is passed into the job using
-ArgumentListkeyword.
@()is a sub-expression operator. It can be used to create empty array by
arr=@(). It is used to run algorithmic operation (multiplication) here.
for($i=4;$i -le 10; $i++){Start-Job {conda activate ENV d:; cd “D:\PATH”; python .\SCRIPT.py –DS $args[0]} -ArgumentList @($i*10);}
File management
rename with PS
Replace the same section of strings with Powershell.
Ref: {StackExchange}
get-childitem *.mp3 | foreach { rename-item $_ $_.Name.Replace("Radiohead -", "") }
Ref: {Link}
Get-ChildItem *.jpg | Rename-Item -NewName { $_.Name -replace '.jpg','.png' }
or a more linux way, thanks to the aliases in powershell
$files=ls; foreach($fs in $files){$fn = $fs.Name; mv $fn $fn.replace('Pattern A', 'Pattern B')}
A demo of for loop and if statement
foreach($f in ls){echo $f.Name; $count=(ls $f|measure).Count; if($count -ne 10000){echo $count}}
- $(var_name) to specify variables
|
is pipeline,measure
return number of rows, similar tocount
- powershell comparison operators:
-eq
is equal and-ne
not equal foreach
syntax:foreach(condition){operations}if
syntax,if(condition){statement}
Zip & Unzip
powershell
foreach($fs in $files){Expand-Archive $fs -DestinationPath "D:\AYX\Rawdon\Supplement_231005\ehm New data\unzipped\$fs.Name.replace('.zip', '')" }
similarly, to zip, use
Compress_archivecommand as
Compress_Archive -Path {Files, add to the list} -DestinationPath {YourName.zip}
dos with 7zip
When unzipping 7z files but do not have permission to install, one can use 7zr.exe {Download Link}.
But one must call it with full directory. I tried doskey that claims to resemble ‘alias’ in linux, but it returns help docs only.
{full_dir}\7zr.exe \? # see help
doskey {full_dir}\7zr.exe 7zr # the alias