Working with the Yandex Cloud CLI and API in Microsoft Windows
Yandex Cloud services provide a variety of interfaces for implementing your projects. Usually, Yandex Cloud services have a web interface in the management console
The Yandex Cloud documentation contains step-by-step guides for working with different interfaces. Sample CLI commands and API requests usually use the Bash syntax. You can run them as is in the Linux console, macOS terminal, or WSL
If you use Yandex Cloud in Windows with no WSL, sample CLI commands and API requests may run incorrectly. To use the examples provided in the documentation, install the Bash interface on your PC, create a VM to work with Yandex Cloud, or adapt commands to the syntax of the Windows command shell you use. For more information on Windows command shells, see the official Microsoft documentation.
How to run Bash commands in the Windows terminal
Windows Subsystem for Linux
-
To check whether WSL is installed on your PC, run the following command in cmd or PowerShell:
wsl -u root
If WSL is installed, the terminal will switch to Bash mode:
root@<machine_name>:/mnt/c/Users/<username>#
-
If not, install
WSL and repeat the previous step.
If you are using Windows without WSL, you can install a Bash emulator, such as Git Bash
How to adapt your code to command line shells native for Windows
If you want to run your Bash code in cmd or PowerShell, bring it to the appropriate format:
- Replace basic syntax elements.
- Check the format of equivalent commands.
- Replace other commands with their equivalents.
- Check the names of system variables and auxiliary syntax.
Replace basic syntax elements
-
Replace the directory separator in file paths:
/
in Linux and\
in Windows. -
Replace line break characters. Instead of
\
, use^
in cmd and`
in PowerShell. -
Check out the rules for using all kinds of quotation marks, as they are handled differently in Linux
and PowerShell . -
Check out the syntax for using variables.
Examples of using variables
Action Linux Windows cmd PowerShell Writing a variable export VAR="1"
set VAR="1"
$Env:VAR="1"
Reading a variable ${VAR}
%VAR%
$Env:VAR
Check the format of equivalent commands
Many commands in cmd and PowerShell have aliases similar to Bash commands. For example, the Set-Location
PowerShell command (change to a different directory) will be run if you type cd
in the terminal.
If the code execution fails:
-
Check if the Bash command has an equivalent in your terminal. For example, in PowerShell, run:
alias
This will output a list of all aliases defined in the system settings:
CommandType Name Version Source ----------- ---- ------- ------ Alias % -> ForEach-Object Alias ? -> Where-Object Alias ac -> Add-Content Alias asnp -> Add-PSSnapin Alias cat -> Get-Content Alias cd -> Set-Location Alias CFS -> ConvertFrom-String 3.1.0.0 Microsoft.PowerShell.Utility Alias chdir -> Set-Location Alias clc -> Clear-Content Alias clear -> Clear-Host ...
For a complete list of aliases, see the
alias
command output. -
Make sure the command in your command shell performs the same function as its equivalent Bash command.
-
Check the command format: the alias keys may differ. For example, the
ls
command has an alias in PowerShell, but the argument keys differ.
Replace other commands with their equivalents
-
If a command has no alias, find its equivalent.
List of equivalent commands in Linux, cmd, and PowerShell
Action Linux Windows cmd Windows PowerShell List files in a directory ls -s
dir
dir
,ls
,Get-ChildItem
List files in a directory and sub-directories ls -R
tree
ls
,Get-ChildItem
Create a file touch
copy nul >
New-Item
Rename a file mv
ren
ren
,Rename-Item
Copy a file cp
copy
copy
,Copy-Item
Move a file mv
move
move
,Move-Item
Delete a file rm
del
del
,Remove-Item
Compare file content diff
fc
diff
,Get-Content
,Compare-Object
Find file strings or object properties grep
find
Select-String
,Where-Object
Output command help man <command>
<command> /?
Get-Help <command>
Output the name of the current directory pwd
cd
$PWD
Create directory mkdir
md
mkdir
,New-Item
Output current date and time date
time
Get-Date
Configure file access policies chown
,chmod
attrib
attrib
,Set-XFileOwner
Schedule the command run cron
at
schtasks
,Register-ScheduledJob
Access a web resource over HTTP wget
wget
Invoke-WebRequest
-
If a command has no direct equivalent, you can perform the same action using a combination of commands or other methods. For example, to convert a file to Base64
in PowerShell, use the static Convert method:[Convert]::ToBase64String([IO.File]::ReadAllBytes(''<filename>''))
. -
Consider the specifics of your command shell. For example, PowerShell works with objects and their properties rather than text. Therefore, instead of searching by
grep
, you will need to use theWhere-Object
PowerShell command in most cases. It handles objects with required property values. To search for a sequence of characters, use theSelect-String
command.
Check the names of system variables and auxiliary syntax
- Check the names of environment variables. For example, instead of the
USER
environment variable used in Linux, theUSERNAME
variable is used in Windows. - If your code contains comparison, selection, or loop statements, change the syntax to meet the requirements of your command shell. To learn more about the difference between the Bash and PowerShell auxiliary syntax, see Bash vs PowerShell Cheat Sheet
.