powershell-2.0

Save hash table in PowerShell object notation (PSON)

泄露秘密 提交于 2019-11-26 06:03:46
问题 The question Loading a PowerShell hashtable from a file? documents how to load a file that contains a hashtable in PSON format into a variable, but how does one save a hashtable to a file in PSON format? Hashtable: @{ \"name\" = \"report 0\" \"parameters\" = @( @{\"name\" = \"parameter 0\"; \"default\" = 1; \"values\"=1,2,3,4}, @{\"name\" = \"parameter 1\"; \"default\" = \'A\'; \"values\" = \'A\',\'B\',\'C\'} ) } 回答1: Try the *-CliXml cmdlets. To save the object: @{ "name" = "report 0"

Is there a way to make a PowerShell script work by double clicking a .ps1 file?

删除回忆录丶 提交于 2019-11-26 05:57:38
问题 I am distributing a PowerShell script to my team. The script is to fetch an IP address from the Vsphere client, make an mstsc connection, and log it in a shared file. The moment they used the script they got to know the IP address of machine. After that, they always tend to use mstsc directly instead of running the PowerShell script. (As they are using mstsc I am not able to know whether they are using the VM frequently or not.) Mainly they are telling me that running PowerShell is not

Delete files older than 15 days using PowerShell

╄→尐↘猪︶ㄣ 提交于 2019-11-26 04:59:03
问题 I would like to delete only the files that were created more than 15 days ago in a particular folder. How could I do this using PowerShell? 回答1: The given answers will only delete files (which admittedly is what is in the title of this post), but here's some code that will first delete all of the files older than 15 days, and then recursively delete any empty directories that may have been left behind. My code also uses the -Force option to delete hidden and read-only files as well. Also, I

What's the best way to determine the location of the current PowerShell script?

老子叫甜甜 提交于 2019-11-26 01:36:59
问题 Whenever I need to reference a common module or script, I like to use paths relative to the current script file. That way, my script can always find other scripts in the library. So, what is the best, standard way of determining the directory of the current script? Currently, I\'m doing: $MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition) I know in modules (.psm1) you can use $PSScriptRoot to get this information, but that doesn\'t get set in regular scripts (i.e.