PowerShell 初探

本文介绍了PowerShell的基础知识,包括运行方式、变量定义、函数创建及使用条件与循环语句的方法。此外,还提供了如何更改PowerShell的执行策略来运行脚本的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.PowerSehl 运行方式

命令控制台窗口中,输入powershell进入Powershell命令后环境。Windows中可以通过get-executionpolicy命令查看PowerShell运行的方式。

Windows中PowerShell默认运行方式是Restrict,也就是默认禁止PowerShell脚本执行。更改脚本执行方式可以通过set-executionpolicy unrestricted

2.Powershell变量

PowerShell中变量的命名是以$开头,字符数字等命名.如$str="Hello PowerShell".

3.PowerShell中函数

PowerShell中函数定义以Function为关键字。

Function LogW($str)

{

echo $str

 }

$logpath="d:\$(gc env:computername).log"  

definition:

Function LogWrite

{

Param([string]$logstring)

Add-content $logpath -value $logstring

 }

Use method 

LogWrite "Hello Powershell"

LogWrite "PS demo"

3. 条件/控制/循环语句

if(test-path $logpath)

{

echo "$logpath exists"

 }

else

{

echo "$logpath not exists"

 }

$n=2

switch

{

0 {"equal 0"}

1 {"equal 1"}

2 {"equal 2"}

  default {" unknown"}

 }

$arr=1..5

foreach ($n in $arr)

{

$n * $n

 }

4. 返回值

函数返回值,函数体内直接return 返回值,程序返回值exit 返回值.

...



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值