SoFunction
Updated on 2025-03-10

Explore PowerShell (V) Basics of PowerShell

In PowerShell, we can easily interact with data and objects. In order to simplify our access to external data, PowerShell allows us to operate data, objects, etc. like operating drives and files.

Use this command to view our existing Providers:

get-psprovider 

In fact, each Provider is a dynamic link library (.dll), which can also be called a "snap unit" in PowerShell. In the snap-in, there is detailed code to implement our various operations. PowerShell even allows us to write our own provider:/en-us/library/cc136763(VS.85).aspx

We have many ways to operate data, etc., for example:

cmdlet Function cmd command alias
get-location Current directory pwd gl
set-location Change the operation directory cd,chdir sl
new-item Create a new file or folder none ni
rename-item Rename rn rni
copy-item copy copy cpi
move-item move move mi
remove-item delete del ri
set-item Set content none si
clear-item Delete content none cli
mkdir Create a new folder md none
set-content Set content none sc
get-content Get content type gc

PSDriver

You can get the current drive list through the PSDriver in PowerShell. Basically all providers have a unique PSDriver, but the file system and registry are exceptions. Check it through the following command:

get-psdriver 

Note: The "Name" in the figure above does not display a colon (:) when it is displayed, but when we use it, we must add it, such as alias:, c:, etc.

Environment variables

The environment variables in PowerShell are similar to the "set" command in CMD. You can use the following command to view the settings of the environment variables on the current machine:

cd env:  

ls 

We try to get the values ​​of some environment variables.

ls OS

Show all properties:

ls OS | format-list * 

Try creating a new environment variable:

new-item -path . -Name New -Value “New”<enter> 



Next, let's continue to understand the "Mode" displayed when "get-childitem".

First position: d represents folder - represents directory

The last four bits (a, r, h, s): a archive r read-only h hides s system - means that this bit is not set

By default, "get-childitem" does not display hidden files. Therefore, we need to use the parameter "-force".

ls -force <enter> 

So what about the newly created file? Let's try the following command:

new-item newfile 

At this time, you need to enter the file type, namely "file" and "directory", and we just need to use "f" and "d".

Or, use parameters when creating new:

 

Function in PowerShell

PowerShell provides an engine for us to call various functions to implement functions. Let's take a look at the built-in functions:

cd function:  

ls 

Next, use the cmdlets I introduced before to see the content of the "clear-host" function:

get-content clear-host 

Therefore, for convenience, we can create some code blocks as much as possible to implement specific functions, and all we have to do is call it. Isn't it very convenient? In the following tutorial, I will introduce in detail the writing of functions to implement function calls.

Registration form

In PowerShell we can operate the registry like a file. PowerShell allows us to operate HKCU and HKLM.

HKCU:HKEY_CURRENT_USER

HKLM:HKEY_LOCAL_MACHINE

Try the following command:

cd hklm:  

cd software  

ls  

Certificate

cd cert:  

ls 

Export and take a look~

ls -Recurse | Export-CSV “d:\”

Okay, that's all for this section. The things are complicated and trivial, but these will greatly deepen our understanding and understanding of PowerShell to help us learn better in the future.

It’s the weekend, I wish you all a happy weekend! Thank you for your attention to this blog~ Thank you!

new-item newfile -type f