Windows

2021-08-22

  • 1 CMD
    • 1.1 Basics
      • 1.1.1 Extract filename and extension of input file parameter
      • 1.1.2 Open a file with the default application in WSL
      • 1.1.3 Use start with space in the path
    • 1.2 Tips
      • 1.2.1 Run two commands in one line
  • 2 Powershell
    • 2.1 Basics
      • 2.1.1 Read line by line
      • 2.1.2 Environment variable
      • 2.1.3 Pipeline
      • 2.1.4 Concatenating a variable and a string literal without a space
      • 2.1.5 Extract filename and extension from path
      • 2.1.6 Meaning of $_
      • 2.1.7 Current directory of the script executed
      • 2.1.8 Combine a path with a child path
      • 2.1.9 Comment out a line
      • 2.1.10 Run multiple commands on a single line
      • 2.1.11 Equivalent command of which
      • 2.1.12 Create the symbolic link
      • 2.1.13 Create a directory if it does not exist
      • 2.1.14 Capture the output of a command and assign it to a variable
      • 2.1.15 Show the command history
    • 2.2 Tips
      • 2.2.1 Profile location
      • 2.2.2 Set HTTP proxy
  • 3 Network
    • 3.1 Basics
      • 3.1.1 Refresh the DNS cache
    • 3.2 Proxy
      • 3.2.1 CMD Set Proxy
      • 3.2.2 Use Proxifier to set shadowsocks as the global proxy
      • 3.2.3 conda connection issue
  • 4 System
    • 4.1 WSL2
      • 4.1.1 X11 forwarding
      • 4.1.2 Use the proxy of the host in WSL2
      • 4.1.3 No network in WSL2
      • 4.1.4 Reduce disk size
    • 4.2 System setting
      • 4.2.1 Rename the user folder in Windows 10
      • 4.2.2 How to Remove Programs from the “Open With” Context Menu in Windows
      • 4.2.3 Enable Group Policy Editor (Gpedit.Msc) In Windows 10 Home Edition
      • 4.2.4 Disable indexing removable drives
  • 5 Others
    • 5.1 SSH
      • 5.1.1 Windows SSH: Permissions for ‘private-key’ are too open
    • 5.2 PDF
      • 5.2.1 Convert CAJ to PDF

1 CMD

1.1 Basics

1.1.1 Extract filename and extension of input file parameter

set _filename=%~n1
set _extension=%~x1
  • %~n1 - Expand %1 to a file Name without file extension.
  • %~x1 - Expand %1 to a file eXtension only.

Reference

1.1.2 Open a file with the default application in WSL

cmd.exe /C start <file>

Note: you need to replace /mnt/c with C:.

1.1.3 Use start with space in the path

Use an additional quotes.

start "" "Y:\foo bar\baz"

1.2 Tips

1.2.1 Run two commands in one line

dir & echo foo

If you want the second command to execute only if the first exited successfully:

dir && echo foo

2 Powershell

2.1 Basics

2.1.1 Read line by line

foreach($line in Get-Content .\file.txt) {
    if($line -match $regex){
        # Work here
    }
}

2.1.2 Environment variable

The environment variable is provided by Env:. To list all the environment variables, do the following

Set-Location Env:
Get-ChildItem

2.1.3 Pipeline

Use the symbol |.

For example, convert the line ending in all the markdown files in a folder to the UNIX format.

Get-ChildItem -Path .\ -Filter *.md -Recurse -File -Name | ForEach-Object { dos2unix $_ }

2.1.4 Concatenating a variable and a string literal without a space

$MyVariable = "Some text"
Write-Host "${MyVariable}NOSPACES"

2.1.5 Extract filename and extension from path

$PSCommandPath
(Get-Item $PSCommandPath ).Extension
(Get-Item $PSCommandPath ).Basename
(Get-Item $PSCommandPath ).Name
(Get-Item $PSCommandPath ).DirectoryName
(Get-Item $PSCommandPath ).FullName
$ConfigINI = (Get-Item $PSCommandPath ).DirectoryName+"\"+(Get-Item $PSCommandPath ).BaseName+".ini"

2.1.6 Meaning of $_

This is the variable for the current value in the pipe line, which is called $PSItem in Powershell 3 and newer.

2.1.7 Current directory of the script executed

$PSScriptRoot

2.1.8 Combine a path with a child path

Join-Path -Path "path" -ChildPath "childpath"

2.1.9 Comment out a line

Use #

2.1.10 Run multiple commands on a single line

Use a semicolon to chain commands in PowerShell:

ipconfig /release; ipconfig /renew

2.1.11 Equivalent command of which

Get-Command

2.1.12 Create the symbolic link

New-Item -ItemType SymbolicLink -Path "Link" -Target "Target"

In addition, you can call the mklink provided by cmd, from PowerShell to make symbolic links:

cmd /c mklink c:\path\to\symlink c:\target\file

You must pass /d to mklink if the target is a directory.

cmd /c mklink /d c:\path\to\symlink c:\target\directory

2.1.13 Create a directory if it does not exist

Try the -Force parameter:

New-Item -ItemType Directory -Force -Path C:\Path\That\May\Or\May\Not\Exist

2.1.14 Capture the output of a command and assign it to a variable

$OutputVariable = (Shell command)

2.1.15 Show the command history

Get the location of the history file by the following command

(Get-PSReadlineOption).HistorySavePath

Then open the path in a text editor to see the full history.

Get-History shows only the history in the current session.

2.2 Tips

2.2.1 Profile location

Use the following command to check the current values of $PROFILE.

PS C:\Users\J.Shi> $PROFILE | Get-Member -Type NoteProperty


   TypeName: System.String

Name                   MemberType   Definition
----                   ----------   ----------
AllUsersAllHosts       NoteProperty string AllUsersAllHosts=C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    NoteProperty string AllUsersCurrentHost=C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    NoteProperty string CurrentUserAllHosts=C:\Users\J.Shi\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost NoteProperty string CurrentUserCurrentHost=C:\Users\J.Shi\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

For reference, check The $PROFILE variable.

2.2.2 Set HTTP proxy

For reference, check 使用PowerShell Profile快速设置 HTTP 代理.

This script has already saved to the PowerShell profile.

3 Network

3.1 Basics

3.1.1 Refresh the DNS cache

ipconfig /flushdns

3.2 Proxy

3.2.1 CMD Set Proxy

set HTTP_PROXY=http://127.0.0.1:1080

IP and port should be adjusted to the real situation. Check IE -> Internet Options -> Connections -> LAN settings. For Shadowsocks, it is normally http://127.0.0.1:1080.

For Powershell, the configuration seems to be so complex.

3.2.2 Use Proxifier to set shadowsocks as the global proxy

使用Proxifier把shadowsocks代理转为真·全局(类VPN)

3.2.3 conda connection issue

The connection issue is as follows,

(base) C:\Users\avnis>conda update conda
Collecting package metadata (current_repodata.json): failed

CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://repo.anaconda.com/pkgs/main/win-64/current_repodata.json
Elapsed: -

Copy the following files

libcrypto-1_1-x64.*
libssl-1_1-x64.*

from D:\Anaconda3\Library\bin to D:\Anaconda3\DLLs.

4 System

4.1 WSL2

4.1.1 X11 forwarding

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

Reference: How to set up working X11 forwarding on WSL2

4.1.2 Use the proxy of the host in WSL2

First obtain the host IP: awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null Then set up the environment variables:

export http_proxy='http://<Windows IP>:<Port>'
export https_proxy='http://<Windows IP>:<Port>'

4.1.3 No network in WSL2

cmd as admin:

wsl --shutdown
netsh winsock reset
netsh int ip reset all
netsh winhttp reset proxy
ipconfig /flushdns

Windows Search > Network Reset

Restart Windows

4.1.4 Reduce disk size

The size of the virtual hard drive keeps on increasing automatically and if you delete a file from the linux file system, the space occupied by the VHD is still the same. We need a way to manually free up this space from Windows 10.

wsl --shutdown

4.2 System setting

4.2.1 Rename the user folder in Windows 10

For reference: How to rename the User folder in Windows 10?

Basically, simply rename the user folder and edit the registry.

4.2.2 How to Remove Programs from the “Open With” Context Menu in Windows

Edit the registry. Check this link.

4.2.3 Enable Group Policy Editor (Gpedit.Msc) In Windows 10 Home Edition

The Group Policy Editor is not available to any Windows Home users, be it Windows XP, Windows 7, Windows 8.1 or Windows 10.

This is a serious drawback as we need the group policies even for managing settings of the local computer. The local group policy is better than changing the settings through Windows Registry which is much riskier. Most group policy settings can revert easily, while Registry editing can have adverse effects on the system.

Use this simple PowerShell script to install the disabled Group Policy feature in the Windows 10 Home edition. Although a restart is not required, if the policies are not working, you should restart the computer once.

Open the Run dialog by pressing Windows key + R. Type gpedit.msc and press the Enter key or OK button. This should open gpedit in Windows 10 Home.

Reference: How To Enable Group Policy Editor (Gpedit.Msc) In Windows 10 Home Edition

4.2.4 Disable indexing removable drives

In the Local Group Policy Editor, navigate to the location Computer Configuration\Administrative Templates\Windows Components\Search. Edit Do not allow locations on removable drives to be added to libraries to enable it.

Reference: Enable or Disable Adding Locations on Removable Drives to Index and Libraries in Windows 10

5 Others

5.1 SSH

5.1.1 Windows SSH: Permissions for ‘private-key’ are too open

For reference, check Windows SSH: Permissions for ‘private-key’ are too open.

You locate the file in Windows Explorer, right-click on it then select “Properties”. Navigate to the “Security” tab and click “Advanced”.

Change the owner to you, disable inheritance and delete all permissions. Then grant yourself “Full control” and save the permissions. Now SSH won’t complain about file permission too open anymore.

It should end up looking like this:

5.2 PDF

5.2.1 Convert CAJ to PDF

Directly go to the original webpage of this CAJ. Copy the CAJ link and replace cajdown with pdfdown to download a PDF version.

.
Created on 2021-08-22 with pandoc