powershell-3.0

PowerShell script to move files and folders including subfolders from one location to another older than x days

冷暖自知 提交于 2019-12-04 01:03:59
I developed a PowerShell script, and it's working absolutely fine. The only challenge is the files in the subfolders are not getting moved to the destination. get-childitem -Path "\\servername\location" | where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} | move-item -destination "C:\Dumps" I am unable to customize the script further. Musaab Al-Okaidi Use the -Recurse option on the Get-ChildItem command to get through to the files in the sub folders and then move each individually by piping the collection to Move-Item Get-ChildItem -Path "C:\Test" -Recurse | Where-Object {$_

How can I check if a file is older than a certain time with PowerShell?

∥☆過路亽.° 提交于 2019-12-04 00:16:44
How can I check in Powershell to see if a file in $fullPath is older than "5 days 10 hours 5 minutes" ? ( by OLD, I mean if it was created or modified NOT later than 5 days 10 hours 5 minutes) Here's quite a succinct yet very readable way to do this: $lastWrite = (get-item $fullPath).LastWriteTime $timespan = new-timespan -days 5 -hours 10 -minutes 5 if (((get-date) - $lastWrite) -gt $timespan) { # older } else { # newer } The reason this works is because subtracting two dates gives you a timespan. Timespans are comparable with standard operators. Hope this helps. Test-Path can do this for you

How to copy folder with subfolders? [duplicate]

十年热恋 提交于 2019-12-03 15:34:07
问题 This question already has answers here : How to use PowerShell copy-item and keep structure (9 answers) Closed 2 years ago . This script works perfectly in PowerShell. It copies all files with specific type. But I want copy files with it folders & subfolders. $dest = "C:\example" $files = Get-ChildItem -Path "C:\example" -Filter "*.msg" -Recurse foreach ($file in $files) { $file_path = Join-Path -Path $dest -ChildPath $file.Name $i = 1 while (Test-Path -Path $file_path) { $i++ $file_path =

PowerShell - Setting $ErrorActionPreference for the entire script

假如想象 提交于 2019-12-03 11:40:20
问题 I've been giving PowerShell (v3.0) a shot for the first time today, and became enormously frustrated with the strange way some of its error-handling concepts are implemented. I wrote the following piece of code (using the Remote Registry PowerShell Module) try { New-RegKey -ComputerName $PCName -Key $Key -Name $Value Write-Host -fore Green ($Key + ": created") } catch { Write-Host -fore Red "Unable to create RegKey: " $Key Write-Host -fore Red $_ } (This is just a snippet) Apparently the

Use cases of [ordered], the new PowerShell 3.0 feature

核能气质少年 提交于 2019-12-03 11:34:28
PowerShell 3.0 CTP1 introduces a new feature [ordered] which is somewhat a shortcut for OrderedDictionary . I cannot imagine practical use cases of it. Why is this feature really useful? Can somebody provide some useful examples? Example: this is, IMHO, rather demo case than practical: $a = [ordered]@{a=1;b=2;d=3;c=4} (I do not mind if it is still useful, then I am just looking for other useful cases). I am not looking for use cases of OrderedDictionary , it is useful, indeed. But we can use it directly in v2.0 (and I do a lot). I am trying to understand why is this new feature [ordered]

Getting all open PS Sessions on a remote server (from new console window)

时间秒杀一切 提交于 2019-12-03 10:53:52
I can start 5 new PS sessions on a remote server and see them all by running Get-PSSession PS C:\> New-PSSession -ComputerName MyServerName Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 Session1 MyServerName Opened Microsoft.PowerShell Available [repeat 4 more times] As expected, when I try to open a 6th session, I get the error saying that's a no-no (due to PoswerShells default limit of 5 concurrent remote PSSessions). But running Get-Session shows all 5 sessions so all is working as it should be so far: PS C:\> New

Copy-item Files in Folders and subfolders in the same directory structure of source server using PowerShell

不打扰是莪最后的温柔 提交于 2019-12-03 10:40:51
问题 I am struggling really hard to get this below script worked to copy the files in folders and sub folders in the proper structure (As the source server). Lets say, there are folders mentioned below: Main Folder: File aaa, File bbb Sub Folder a: File 1, File 2, File 3 Sub Folder b: File 4, File 5, File 6 Script used: Get-ChildItem -Path \\Server1\Test -recurse | ForEach-Object { Copy-Item -LiteralPath $_.FullName -Destination \\server2\test | Get-Acl -Path $_.FullName | Set-Acl -Path "\\server2

Remove a Member from a PowerShell Object?

假装没事ソ 提交于 2019-12-03 10:29:03
问题 I need to remove a member (specifically, a NoteProperty) from an object. How do I accomplish this? 回答1: Select-Object with ExcludeProperty is good for removing a property from a collection of objects. For removing a property from a single object this method might be more effective: # new object with properties Test and Foo $obj = New-Object -TypeName PSObject -Property @{ Test = 1; Foo = 2 } # remove a property from PSObject.Properties $obj.PSObject.Properties.Remove('Foo') 回答2: I don't think

Compiling and running java application using powershell

╄→尐↘猪︶ㄣ 提交于 2019-12-03 10:14:28
问题 I am trying to compile and a sample Helloworld.java file. I have my jdk installed in C:\Program Files\jdk1.7\bin. And I have my Helloworld.java in C:\Helloworld.java I am actually a novice in both powershell and java. I got some examples from web regarding this but many of them advice to run it like this: java.exe -classpath $Env:CLASSPATH C:\Helloworld.java But when I give this in powershell I get an error called 'CLASSPATH' is not defined even after adding it in env variables. And when I

PowerShell Get-DiskUsage CmdLet: how to list from a different drive/directory?

爷,独闯天下 提交于 2019-12-03 10:11:27
I'm a relative newbie on PowerShell, wanted to know a bit more about functions, CmdLet and human readable values. Usually for learning new things, looking at what others do works well. So I went looking and bumped into the source for a Get-DiskUsage CmdLet. I can . source this into PowerShell, then call the function. Somehow, it always uses the current directory for getting the results no matter how I call it. What am I missing that it will not take the given -Path parameter? PS D:\bin> . .\DiskUsage.ps1 PS D:\bin> Get-DiskUsage -Path D:\ -h Size Folder ---- ------ 405M PS D:\bin> Get